这是日记的代码。我希望用户在日记中写一些东西,这将是第一页,并将被放入列表中。写完后,应该开始下一页,他会在下一页写一些东西等。 我一遍又一遍地犯错,我不知道为什么。写完"输入"我收到错误。我是java新手,所有编程/编码都是新手(如果它是相同的)。对不起,如果这是一个非常愚蠢的问题,我问:/ 谢谢你的建议。我感谢一切,因为我想尽可能多地为我的大学学习一年。
import java.util.ArrayList;
import java.util.Scanner;
public class NotizbuchKlasse{
public static void Pages(){
System.out.println("Tag 1 : Write something in your diary.");
System.out.println("Write enter if you are done writing.");
ArrayList<String> List = new ArrayList<String>();
String ListInList;
Scanner write;
do{
write = new Scanner(System.in);
ListInList = write.next();}
while (! ListInList.equals("enter"));
System.out.println("This is now your page. Your page is gonna be
created after writing something new.");
String y = ListInList;
List.add(y);
write.close();
Pages();
}
public static void main(String[]Args){
Pages();
}
}
day 1 : Write something in your diary.
Write enter if you are done writing.
hello diary
enter
This is now your page. Your page is gonna be created after writing something
new.
Exception in thread "main" day 1 : Write something in your diary.
Write enter if you are done writing.
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at NotizbuchKlasse.Pages(NotizbuchKlasse.java:14)
at NotizbuchKlasse.Pages(NotizbuchKlasse.java:20)
at NotizbuchKlasse.main(NotizbuchKlasse.java:38)
答案 0 :(得分:2)
首先,当您阅读操作符号时,您需要将其作为String读取,因为它不是整数。
此外,在比较字符串时,您应该使用equals
方法。
最后,在执行除法时,您应该将其中一个操作数转换为float以获得浮点结果(否则您将获得转换除法 - int - )。
这就是说,你的代码应该是这样的:
import java.util.Scanner;
public class Taschenrechner {
public static void calculator() {
Scanner input = new Scanner(System.in);
// Read input numbers
System.out.println("Give me the 2 numbers first: ");
int x = input.nextInt();
int y = input.nextInt();
// Read operation
System.out.println("Now give me the operation (+,-,*,/): ");
String operation = input.next();
// Perform operation
float result = 0;
if (operation.equals("+")) {
result = x + y;
} else if (operation.equals("-")) {
result = x - y;
} else if (operation.equals("*")) {
result = x * y;
} else if (operation.equals("/")) {
// Add float casting to one of the operands to obtain a float result
result = x / ((float) y);
} else {
System.err.println("ERROR: Unrecognised operation " + operation);
}
// Print result
System.out.println("Result: " + result);
// Close scanner
input.close();
}
public static void main(String[] args) {
calculator();
}
}
请注意:
equals
方法比较字符串。if
更改if-else
,else
,switch-case
。答案 1 :(得分:1)
Give me the 2 numbers first:
1
2
Now give me the operation (+,-,*,/):
+
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Taschenrechner.Operationen(Taschenrechner.java:10)
at Taschenrechner.main(Taschenrechner.java:30)
意思是:
输入+
后,引发了Exception
。它是InputMismatchException
。它在第30行的Taschenrechrer.java中被抛入(...)Scanner#nextInt
。
在提供的代码中,还有三个不同的问题。一个是你应该区分"x"
和''
。第一个是String
,另一个是char
。
另一个事实是,您可能期望浮动除法而不是整数,因此您必须在除法之前添加1.0
。否则,结果将始终为整数。
但最重要的是,扫描仪与输入流相关联。流是浮动的,不能回退的。这意味着,您必须读取输入一次,然后比较它是什么。调用a.next()
三次将导致扫描仪读取3个不同的输入。
public class Taschenrechner {
public static void calculator(){
Scanner a = new Scanner(System.in);
System.out.println("Give me the 2 numbers first: ");
int x = a.nextInt();
int y = a.nextInt();
System.out.println("Now give me the operation (+,-,*,/): ");
String op = a.next();
if (op.equals("+"){
float result = x + y;
System.out.println("result: " + result);
}
else if (op.equals("-")){
float result = x - y;
System.out.println("result: " + result);
}
else if (op.equals("*")){
float result = x * y;
System.out.println("result: " + result);
}
else{
float result = 1.0 * x / y;
System.out.println("result: " + result);
}
a.close();
}
public static void main(String []args) {
calculator();
}
}
答案 2 :(得分:1)
我会查看您的代码并提出一些重构建议。
if (a.nextInt()=='+'){
float result = x + y;
System.out.println("result: " + result);
}
else if (a.nextInt()=='-'){
float result = x - y;
System.out.println("result: " + result);
}
else if (a.nextInt()=='*'){
float result = x * y;
System.out.println("result: " + result);
}
else{
float result = x / y;
System.out.println("result: " + result);
}
a.close();
}
如果操作不等于您编码的内容,则每次都读取操作。因此,如果用户想要输入操作 - ,则必须输入与 + 不同的第一次操作,然后输入 - 。
其次,将硬编码常量移动到私有静态字段(如果在其他类中使用它们,则为公共静态)是一种很好的做法。
而不是if-else,更喜欢使用switch(用于基元,字符串,枚举) - 更好的样式和性能(JVM优化切换)。
这就是我编码的方式
Taschenrechner
public class Taschenrechner {
private static final char ADD = '+';
private static final char SUB = '-';
private static final char MUL = '*';
private static final char DIV = '/';
public static void calculate() {
Scanner reader = new Scanner(System.in);
System.out.println("Give me the 2 numbers first: ");
int x = reader.nextInt();
int y = reader.nextInt();
System.out.println("Now give me the operation (+,-,*,/): ");
final char operation = reader.next(".").charAt(0); //See explanation bellow.
float result = calculateResult(x, y, operation);
System.out.println("result: " + result);
reader.close();
}
private static float calculateResult(float x, float y, char operation) {
switch (operation) {
case ADD:
return x + y;
case DIV:
return x / y;
case SUB:
return x - y;
case MUL:
return x * y;
default:
throw new UnsupportedOperationException(operation + " is not suported."); //Terminate the program with an error. Read about exception handling to understand when it can be used.
}
}
public static void main(String[] args) {
calculate();
}
}
final char operation = reader.next(".").charAt(0);
我只想读一个字。
https://stackoverflow.com/a/13942707/4587961
我还将方法calculator
重命名为calculate
。我是清洁代码做法的忠实粉丝。方法&#39;名字是动词,而类名是名词。
答案 3 :(得分:0)
您正在使用a.nextInt(),它用于将整数作为输入。只需使用a.next()。charAt(0)代替。这是有效的
import java.util.Scanner;
public class Taschenrechner {
public static void calculator() {
Scanner a = new Scanner(System.in);
System.out.println("Give me the 2 numbers first: ");
int x = a.nextInt();
int y = a.nextInt();
System.out.println("Now give me the operation (+,-,*,/): ");
if (a.next().charAt(0) == '+') {
float result = x + y;
System.out.println("result: " + result);
} else if (a.next().charAt(0) == '-') {
float result = x - y;
System.out.println("result: " + result);
} else if (a.next().charAt(0) == '*') {
float result = x * y;
System.out.println("result: " + result);
} else {
float result = x / y;
System.out.println("result: " + result);
}
a.close();
}
public static void main(String[] args) {
calculator();
}
}
答案 4 :(得分:0)
要获得使用nextInt的数学运算符。
a.nextInt()== '+'
这是问题的原因。 此外,使用开关使代码更健壮和可读。
答案 5 :(得分:0)
它会起作用
public static void calculator() {
Scanner a = new Scanner(System.in);
System.out.println("Give me the 2 numbers first: ");
int x = a.nextInt();
int y = a.nextInt();
System.out.println("Now give me the operation (+,-,*,/): ");
String choice = a.next();
if (choice.equals("+")) {
float result = x + y;
System.out.println("result: " + result);
}
if (choice.equals("-")) {
float result = x - y;
System.out.println("result: " + result);
}
if (choice.equals("*")) {
float result = x * y;
System.out.println("result: " + result);
}
if (choice.equals("/")) {
float result = x / y;
System.out.println("result: " + result);
}
a.close();
}
答案 6 :(得分:-1)
if (a.nextInt()=='+'){
float result = x + y;
System.out.println("result: " + result);
}
a.nextInt()期待一个&#34; int&#34;,但你要传递给它一个char。 你有两种可能性。
答案 7 :(得分:-2)
在您的代码中有2个问题
所以,请尝试以下代码
public static void calculator(){
Scanner a = new Scanner(System.in);
System.out.println("Give me the 2 numbers first: ");
int x = a.nextInt();
int y = a.nextInt();
System.out.println("Now give me the operation (+,-,*,/): ");
a.nextLine();
String b = a.next();
if (b.equals("+")){
float result = x + y;
System.out.println("result: " + result);
}
else if (b.equals("-")){
float result = x - y;
System.out.println("result: " + result);
}
else if (b.equals("*")){
float result = x * y;
System.out.println("result: " + result);
}
else{
float result = x / y;
System.out.println("result: " + result);
}
a.close();
}