Try/Catch 循环 return 值
Try/Catch Loop with return value
我是编程新手,所以我只需要一些帮助。我自己想出了一些事情,但现在我迷路了,我会说。请记住,我不是母语人士,对我的错误深表歉意:)
我在我的大学学习 Java,但我们必须自己做练习。
该程序是关于输入 1 和 20 之类的区间,程序对其进行平方,我需要找到正确的答案。我有 3 次尝试获得正确答案。第三次尝试后,我开始询问我是否要尝试另一个号码或结束程序
我得到的实际问题是,"try and catch loop",需要测试我输入的是正数。
public static int prüfung(int ZahlPrüfen) {
while (true) {
try {
ZahlPrüfen = IntervallScanner.nextInt();
if (IntervallScanner.hasNextInt() && ZahlPrüfen > 0) {
return ZahlPrüfen;
} else if (ZahlPrüfen <= 0) {
System.out.println("Bitte Geben Sie eine Positive Zahl ein");
IntervallScanner.next();
}
} catch (InputMismatchException e) {
System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
IntervallScanner.next();
}
}
问题有点在于,如果我输入一个正数(所以一切正确),我必须输入两次!同样,当我输入一个负数时,我需要输入两次。当我输入一个字符串时,我得到一个 "error",所以我猜这工作得很好。它显示如下:
Bitte geben Sie die kleinste positive Zahl des Intervalls ein: -2
-3
Bitte Geben Sie eine Positive Zahl ein
-4
-5
Bitte Geben Sie eine Positive Zahl ein
-6
-7
Bitte Geben Sie eine Positive Zahl ein
2
2
Bitte geben Sie die Größte Zahl des Intervalls ein: 3
Wie lautet die Wurzel aus 4?
并且当我输入正确的数字(如本例中的 2 和 2)时,代码第一次使用,并使用它们来生成随机数,而不是 2 和 3。
我希望你能明白我的意思。如果你们中有人可以帮助我,那就太好了,我被困了 5 个小时,无法弄清楚。我的大脑停止工作了:D
完整代码如下:
public static int zufallszahl = 0;
public static Scanner sc = new Scanner(System.in);
public static Scanner eingabe = new Scanner(System.in);
public static Scanner IntervallScanner = new Scanner(System.in);
public static int AnzahlVersuche = 0;
public static int k = 0;
public static int kleineZahl;
public static int großeZahl;
public static int ZahlPrüfen;
public static int ZahlPrüfung;
public static void main(String[] args) {
System.out.print("Bitte geben Sie die kleinste positive Zahl des Intervalls ein: ");
kleineZahl = prüfung(ZahlPrüfen);
System.out.print("Bitte geben Sie die Größte Zahl des Intervalls ein: ");
großeZahl = prüfung(ZahlPrüfen);
do {
ermittleZufallszahl(großeZahl, kleineZahl);
int quadrad = (int) Math.pow(zufallszahl, 2);
System.out.println("Wie lautet die Wurzel aus " + quadrad + "?");
erneuterVersuch();
} while (k == 1);
}
public static void ermittleZufallszahl(int max, int min) {
zufallszahl = (int) ((Math.random() * (max - min + 1)) + min);
}
public static int erneuterVersuch() {
AnzahlVersuche = 0;
while (AnzahlVersuche <= 2) {
int input = sc.nextInt();
if (input == zufallszahl) {
System.out.println("Das ist richtig,toll!");
System.out.println("Wollen Sie eine andere Zahl probieren? Wenn Ja geben Sie (j) ein, wenn Nein geben Sie (n) ein.");
String eingabe1 = eingabe.nextLine();
switch (eingabe1) {
case "j":
case "J":
k = 1;
return k;
case "n":
case "N":
System.out.println("Programm wird jetzt beendet");
k = 2;
System.exit(0);
}
} else {
System.out.println("Das ist falsch, schade!");
++AnzahlVersuche;
if (AnzahlVersuche == 3) {
System.out.println("Sie haben es mit 3 Versuchen leider nicht geschafft! Die richtige Antwort wäre " + zufallszahl + " gewesen.");
System.out.println("Wollen Sie eine andere Zahl probieren? Wenn Ja geben Sie (j) ein, wenn Nein geben Sie (n) ein.");
String eingabe1 = eingabe.nextLine();
switch (eingabe1) {
case "j":
case "J":
k = 1;
break;
case "n":
case "N":
System.out.println("Programm wird jetzt beendet");
k = 2;
System.exit(0);
}
}
}
}
return k;
}
public static int prüfung(int ZahlPrüfen) {
while (true) {
try {
ZahlPrüfen = IntervallScanner.nextInt();
if (IntervallScanner.hasNextInt() && ZahlPrüfen > 0) {
return ZahlPrüfen;
} else if (ZahlPrüfen <= 0) {
System.out.println("Bitte Geben Sie eine Positive Zahl ein");
IntervallScanner.next();
}
} catch (InputMismatchException e) {
System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
IntervallScanner.next();
}
}
}
你好:)
Ich verstehe das nicht :/
public static int prüfung(int ZahlPrüfen) {
while (true) {
try {
int ZahlPrüfen = IntervallScanner.nextInt();
if (IntervallScanner.hasNextInt() && ZahlPrüfen > 0) {
return ZahlPrüfen;
} else if (ZahlPrüfen <= 0) {
System.out.println("Bitte Geben Sie eine Positive Zahl ein");
IntervallScanner.next();
}
} catch (InputMismatchException e) {
System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
IntervallScanner.next();
}
}
您传递的是 ZahlPrufen
,但您将其替换为 IntervallScanner.nextInt();
。所以传入的值从未被使用过。
您不需要接受参数的方法。
错误在于您在 if 条件中检查 hasNextInt(),它需要用户的另一个输入。您只需要检查刚刚输入的号码的有效性。
public static int prüfung() {
while (true) {
try {
int ZahlPrüfen = IntervallScanner.nextInt();
if (ZahlPrüfen > 0) {
return ZahlPrüfen;
} else {
System.out.println("Bitte Geben Sie eine Positive Zahl ein");
}
} catch (InputMismatchException e) {
System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
IntervallScanner.next();
}
}
}
我是编程新手,所以我只需要一些帮助。我自己想出了一些事情,但现在我迷路了,我会说。请记住,我不是母语人士,对我的错误深表歉意:) 我在我的大学学习 Java,但我们必须自己做练习。 该程序是关于输入 1 和 20 之类的区间,程序对其进行平方,我需要找到正确的答案。我有 3 次尝试获得正确答案。第三次尝试后,我开始询问我是否要尝试另一个号码或结束程序
我得到的实际问题是,"try and catch loop",需要测试我输入的是正数。
public static int prüfung(int ZahlPrüfen) {
while (true) {
try {
ZahlPrüfen = IntervallScanner.nextInt();
if (IntervallScanner.hasNextInt() && ZahlPrüfen > 0) {
return ZahlPrüfen;
} else if (ZahlPrüfen <= 0) {
System.out.println("Bitte Geben Sie eine Positive Zahl ein");
IntervallScanner.next();
}
} catch (InputMismatchException e) {
System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
IntervallScanner.next();
}
}
问题有点在于,如果我输入一个正数(所以一切正确),我必须输入两次!同样,当我输入一个负数时,我需要输入两次。当我输入一个字符串时,我得到一个 "error",所以我猜这工作得很好。它显示如下:
Bitte geben Sie die kleinste positive Zahl des Intervalls ein: -2
-3
Bitte Geben Sie eine Positive Zahl ein
-4
-5
Bitte Geben Sie eine Positive Zahl ein
-6
-7
Bitte Geben Sie eine Positive Zahl ein
2
2
Bitte geben Sie die Größte Zahl des Intervalls ein: 3
Wie lautet die Wurzel aus 4?
并且当我输入正确的数字(如本例中的 2 和 2)时,代码第一次使用,并使用它们来生成随机数,而不是 2 和 3。
我希望你能明白我的意思。如果你们中有人可以帮助我,那就太好了,我被困了 5 个小时,无法弄清楚。我的大脑停止工作了:D
完整代码如下:
public static int zufallszahl = 0;
public static Scanner sc = new Scanner(System.in);
public static Scanner eingabe = new Scanner(System.in);
public static Scanner IntervallScanner = new Scanner(System.in);
public static int AnzahlVersuche = 0;
public static int k = 0;
public static int kleineZahl;
public static int großeZahl;
public static int ZahlPrüfen;
public static int ZahlPrüfung;
public static void main(String[] args) {
System.out.print("Bitte geben Sie die kleinste positive Zahl des Intervalls ein: ");
kleineZahl = prüfung(ZahlPrüfen);
System.out.print("Bitte geben Sie die Größte Zahl des Intervalls ein: ");
großeZahl = prüfung(ZahlPrüfen);
do {
ermittleZufallszahl(großeZahl, kleineZahl);
int quadrad = (int) Math.pow(zufallszahl, 2);
System.out.println("Wie lautet die Wurzel aus " + quadrad + "?");
erneuterVersuch();
} while (k == 1);
}
public static void ermittleZufallszahl(int max, int min) {
zufallszahl = (int) ((Math.random() * (max - min + 1)) + min);
}
public static int erneuterVersuch() {
AnzahlVersuche = 0;
while (AnzahlVersuche <= 2) {
int input = sc.nextInt();
if (input == zufallszahl) {
System.out.println("Das ist richtig,toll!");
System.out.println("Wollen Sie eine andere Zahl probieren? Wenn Ja geben Sie (j) ein, wenn Nein geben Sie (n) ein.");
String eingabe1 = eingabe.nextLine();
switch (eingabe1) {
case "j":
case "J":
k = 1;
return k;
case "n":
case "N":
System.out.println("Programm wird jetzt beendet");
k = 2;
System.exit(0);
}
} else {
System.out.println("Das ist falsch, schade!");
++AnzahlVersuche;
if (AnzahlVersuche == 3) {
System.out.println("Sie haben es mit 3 Versuchen leider nicht geschafft! Die richtige Antwort wäre " + zufallszahl + " gewesen.");
System.out.println("Wollen Sie eine andere Zahl probieren? Wenn Ja geben Sie (j) ein, wenn Nein geben Sie (n) ein.");
String eingabe1 = eingabe.nextLine();
switch (eingabe1) {
case "j":
case "J":
k = 1;
break;
case "n":
case "N":
System.out.println("Programm wird jetzt beendet");
k = 2;
System.exit(0);
}
}
}
}
return k;
}
public static int prüfung(int ZahlPrüfen) {
while (true) {
try {
ZahlPrüfen = IntervallScanner.nextInt();
if (IntervallScanner.hasNextInt() && ZahlPrüfen > 0) {
return ZahlPrüfen;
} else if (ZahlPrüfen <= 0) {
System.out.println("Bitte Geben Sie eine Positive Zahl ein");
IntervallScanner.next();
}
} catch (InputMismatchException e) {
System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
IntervallScanner.next();
}
}
}
你好:)
Ich verstehe das nicht :/
public static int prüfung(int ZahlPrüfen) {
while (true) {
try {
int ZahlPrüfen = IntervallScanner.nextInt();
if (IntervallScanner.hasNextInt() && ZahlPrüfen > 0) {
return ZahlPrüfen;
} else if (ZahlPrüfen <= 0) {
System.out.println("Bitte Geben Sie eine Positive Zahl ein");
IntervallScanner.next();
}
} catch (InputMismatchException e) {
System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
IntervallScanner.next();
}
}
您传递的是 ZahlPrufen
,但您将其替换为 IntervallScanner.nextInt();
。所以传入的值从未被使用过。
您不需要接受参数的方法。
错误在于您在 if 条件中检查 hasNextInt(),它需要用户的另一个输入。您只需要检查刚刚输入的号码的有效性。
public static int prüfung() {
while (true) {
try {
int ZahlPrüfen = IntervallScanner.nextInt();
if (ZahlPrüfen > 0) {
return ZahlPrüfen;
} else {
System.out.println("Bitte Geben Sie eine Positive Zahl ein");
}
} catch (InputMismatchException e) {
System.out.println("Bitte Geben Sie eine Zahl und keinen Buchstaben ein");
IntervallScanner.next();
}
}
}