简体   繁体   English

代码不会启动

[英]Code won't launch

Ok, this code won't launch.好的,此代码不会启动。 It will launch the other class.它将启动另一个类。 Yes I did name the The class This, so no error there.是的,我确实将类命名为 This,所以没有错误。

package com.thekyle.hi;

class This {
    double b;
    int e;
    double val;

    This(double base, int exp) {
        this.b = base;
        this.e = exp;

        this.val = 1;
        if (exp==0) return;
        for (; exp > 0; exp--)
            this.val = this.val * base;
System.out.println(exp);
    }

    double get_pwr() {
        return this.val;
    }
}

So any way to make this launch.所以任何方式来进行这次发射。 Also running configurations doesn't work.运行配置也不起作用。

You don't have main method so your code will not execute.您没有 main 方法,因此您的代码不会执行。 Main method is entry point to your application.so You can write main method in your class as Main 方法是应用程序的入口点,因此您可以在类中将 main 方法编写为

public static void main(String []args)
{
    This obj = new This(2,2);
    System.out.println(get_pwr());
}

Like others have commented, this code will not run because your main method is probably in different class .就像其他人评论的那样,此代码将无法运行,因为您的main方法可能在不同的class It looks like you are trying to make your This class be an object in your main method.看起来您正试图使您的This类成为您的 main 方法中的一个对象。 Here are some code examples and explanations of what I changed.以下是一些代码示例以及我更改内容的说明。

This.java这个.java

  1. All of the this keywords you have put in the class are redundant, so I removed them.您在类中放置的所有this关键字都是多余的,因此我将其删除。
  2. Your for loop is improperly set up.您的for loop设置不正确。 I created it with an index of i which is assigned the value of exp.我用i的索引创建了它,该索引被分配了 exp 的值。
  3. To make a variable equal to itself times something else, you can use *= .要使变量等于自身乘以其他值,您可以使用*= For example, a=a*b is equivalent to a*=b .例如, a=a*b等价于a*=b

     class This { double b; int e; double val; This(double base, int exp) { b = base; e = exp; val = 1; if (exp == 0) { return; } for (int i = exp; i > 0; i--) { val*=base; } } double get_pwr() { return val; } }

MyClass.java我的类

  1. This class contains the main method, which is the method that the Java virtual machine looks for to start.这个类包含main方法,也就是Java虚拟机寻找启动的方法。
  2. I've created an object of your This class.我已经创建了你This类的对象。 You made it's constructor take a double base and an int exp, so I have given it a double and an int.你让它的构造函数接受一个double基数和一个int exp,所以我给了它一个双精度和一个整数。
  3. After creating the object, the calculations are complete.创建对象后,计算就完成了。 You made a getter for the variable val which has the result of your computation.您为具有计算结果的变量val创建了一个getter Simply call the method and print it out, which is the the System.out.println() call does.只需调用该方法并将其打印出来,这就是System.out.println()调用所做的。

     public class myClass { public static void main(String[] args) { This myThis = new This(2, 6); System.out.println(myThis.get_pwr()); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM