我收到此错误 - 无法找到或加载主要类makeachange.Main 每当我尝试运行程序时,我都不明白为什么它会向我显示此错误,因为我真的是新手在Java中。有人可以帮我解决这个问题:
import java.util.Scanner;
package makingachange;
public class Main {
private int[] denom;
Main( int[] denom) {
this.denom = denom;
}
void giveChange(int changeRs) {
System.out.println("\nChange for " + changeRs + " in Rs " + ":");
for(int i = 0; i < denom.length; ++i) { int nb = changeRs / denom[i]; if(nb > 0)
System.out.println(nb + " " + denom[i]);
changeRs %= denom[i];
}
}
public static void main(String[] args) {
int[] Rs = {100,50,20,10,5,1};
Scanner input=new Scanner(System.in);
System.out.println("Enter the purchase amount : ");
int purchaseAmount=input.nextInt();
System.out.println("Enter the amount given by customer : ");
int AmountGivenByCusto=input.nextInt();
if(AmountGivenByCusto<purchaseAmount){
System.out.println("Sorry! you paid less than purchase amount! ");
}else
{
int result=AmountGivenByCusto-purchaseAmount;
Main change1 = new Main( Rs);
change1.giveChange(result);
}
}
}
答案 0 :(得分:0)
首先出现错误
package
语句应该是第一个语句,所以应该是这个
package makingachange;
import java.util.Scanner;
然后你必须先生成包
javac -d . Main.java
java makingchange/Main.java
-d
表示创建包
.
表示生成当前目录的使用路径以生成包
答案 1 :(得分:0)
确保在导入java.util.Scanner
之前编写包定义。仔细检查工作区中的包是否与您在类中定义的包具有完全相同的名称(在本例中为makingachange
)。
以下代码适用于我。
package makingachange;
import java.util.Scanner;
public class Main {
private int[] denom;
Main(int[] denom) {
this.denom = denom;
}
void giveChange(int changeRs) {
System.out.println("\nChange for " + changeRs + " in Rs " + ":");
for (int i = 0; i < denom.length; ++i) {
int nb = changeRs / denom[i];
if (nb > 0)
System.out.println(nb + " " + denom[i]);
changeRs %= denom[i];
}
}
public static void main(String[] args) {
int[] Rs = { 100, 50, 20, 10, 5, 1 };
Scanner input = new Scanner(System.in);
System.out.println("Enter the purchase amount : ");
int purchaseAmount = input.nextInt();
System.out.println("Enter the amount given by customer : ");
int AmountGivenByCusto = input.nextInt();
if (AmountGivenByCusto < purchaseAmount) {
System.out.println("Sorry! you paid less than purchase amount! ");
} else {
int result = AmountGivenByCusto - purchaseAmount;
Main change1 = new Main(Rs);
change1.giveChange(result);
}
}
}