我正在尝试编写一个相当简单的程序,它可以计算出一组硬币可以变成更大的硬币(先进入四分之一,剩下的成为硬币,再剩下剩余的硬币)我之前用c ++编写代码我对java的差异有点不清楚。我不确定编译器错误意味着什么,到目前为止搜索已经出现完全不同的情况,使用相同的错误代码。
public class coins
{
int input = 0;
int current_pennies = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int remainder = 0;
//input is loaded
public static void main(String[] args) {
System.out.println("Please enter the starting number of pennies: ");
}
import java.util.Scanner;
Scanner in = new Scanner(System.in);
input = in.nextInt();
// current pennies is set to input
current_pennies = input;
//quaters is set equal to current pennies divided by 25 and floored.
quarters = current_pennies / 25;
//current pennies is set to current pennies, modulo 25. IE the remainder.
current_pennies = current_pennies % 25;
//dimes is set equal to current pennies divided by 10 and floored.
dimes = current_pennies / 10;
//current pennies is set to current pennies, modulo 10 IE the remainder.
current_pennies = current_pennies % 10;
//dimes is set equal to current pennies divided by 5 and floored.
nickels = current_pennies / 5;
//current pennies is set you current pennies, modulo 5 IE the remainder.
current_pennies = current_pennies % 5;
//outputting the data
public static void main(String[] args) {
System.out.println("Number of Quarter s" + quarters);
System.out.println("Number of Dimes " + dimes);
System.out.println("Number of Nickels " + nickels);
System.out.println("Remaining Pennies " + current_pennies);
}
}
和编译器错误。
----jGRASP exec: javac -g coins.java
coins.java:19: error: illegal start of type
import java.util.Scanner;
^
coins.java:19: error: <identifier> expected
import java.util.Scanner;
^
coins.java:22: error: <identifier> expected
input = in.nextInt();
^
coins.java:27: error: <identifier> expected
current_pennies = input;
^
coins.java:31: error: <identifier> expected
quarters = current_pennies / 25;
^
coins.java:35: error: <identifier> expected
current_pennies = current_pennies % 25;
^
coins.java:39: error: <identifier> expected
dimes = current_pennies / 10;
^
coins.java:42: error: <identifier> expected
current_pennies = current_pennies % 10;
^
coins.java:45: error: <identifier> expected
nickels = current_pennies / 5;
^
coins.java:49: error: <identifier> expected
current_pennies = current_pennies % 5;
^
10 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
答案 0 :(得分:1)
所有导入都放在文件的顶部,否则,看起来你的程序实际代码本质上是正确的,我所做的只是删除了类顶部的硬币变量的冗余初始化为0: / p>
import java.util.Scanner;
public class Coins {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the starting number of pennies: ");
// TO DO: Add some form input verfication to ensure program does not crash (InputMismatchException) if user enters a alphabet letter etc.
int input = scanner.nextInt();
// int variable currentPennies is set to input
int currentPennies = input;
// int variable quarters is set equal to currentPennies divided by 25 and floored.
int quarters = (int) Math.floor(currentPennies / 25);
// currentPennies is set to currentPennies modulo 25, i.e. the remainder.
currentPennies = currentPennies % 25;
// int variable dimes is set equal to currentPennies divided by 10 and floored.
int dimes = (int) Math.floor(currentPennies / 10);
// currentPennies is set to currentPennies modulo 10, i.e the remainder.
currentPennies = currentPennies % 10;
// int variable nickels is set equal to currentPennies divided by 5 and floored.
int nickels = (int) Math.floor(currentPennies / 5);
// currentPennies is set to currentPennies modulo 5, i.e the remainder.
currentPennies = currentPennies % 5;
// Outputting the number of each type of coins
System.out.println("Number of Quarters: " + quarters);
System.out.println("Number of Dimes: " + dimes);
System.out.println("Number of Nickels: " + nickels);
System.out.println("Remaining Pennies: " + currentPennies);
}
}
使用示例:
Please enter the starting number of pennies: 98
Number of Quarters: 3
Number of Dimes: 2
Number of Nickels: 0
Remaining Pennies: 3
尝试 here 。
您可能会感到困惑的另一件事是某些行上的(int)
基本上这是因为Math.floor()
方法返回double
数据类型,如果我们使用它将意味着我们会得到在输出中没有意义的小数点,或者我们需要专门格式化我们的输出,以便不显示那些小数点(比仅仅转换为int
更多的工作)。
如果您想挑战自己,可以尝试实施以下几个扩展程序:
Number of Nickels: 0
行。答案 1 :(得分:0)
你必须将你的导入放在你的课程之上,如下所示。这是java结构。首先导入,然后编写您打算使用导入库的类。
import java.util.Scanner;
public class coins
{
int input = 0;
int current_pennies = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int remainder = 0;
}
答案 2 :(得分:-2)
在java中我们使用方法,在方法中编写所有代码是一种很好的做法。 此外,所有导入都在一开始就进行,以便编译器在开始编译代码之前包含它们。 同样,当你从Java开始时,如果从一开始就遵循正确的命名概念,那就更好了(类名必须始终以大写字母开头)。 如果您了解c ++,那么迁移到java会很容易。 快乐的编码!