[英]How can I repeat a public class in Java?
我花了很長時間尋找在Java中重復一個公共類的可能性,我找到了一種方法,可以繼續並重復另一個while循環。 但是我真的不滿意,有沒有更簡單的方法可以做到這一點? 感謝您的幫助。
import java.util.Random;
import java.util.Scanner;
public class Zufallszahl {
public static void main(String[] args) {
while (1 == 1) {
Random rn = new Random();
Scanner numberScan = new Scanner(System.in);
Scanner restartScan = new Scanner(System.in);
int zahl = rn.nextInt(100 - 1) + 1;
int number = 0;
String restart = "";
int a = 0;
while (number != zahl) {
a++;
System.out.print("Rate eine Zahl zwischen 0 und 100: ");
number = numberScan.nextInt();
if (number == zahl) {
System.out.println("richtig");
System.out.println("Du hast " + a + " Versuche gebraucht!");
System.out.print("Nochmal? j/n: ");
restart = restartScan.nextLine();
}
if (number < zahl) {
System.out.println("zu klein");
}
if (number > zahl) {
System.out.println("zu gross");
}
}
if (restart.equalsIgnoreCase("j")) {
continue;
} else if (restart.equalsIgnoreCase("n")) {
System.out.println("Tschüss!");
return;
}
}
}
}
關於重新啟動-除非您將其提取到對象中,否則這似乎很奇怪。 因此,解決方案1可能是只是循環運行,並通過檢查第二個循環中的重新啟動來擺脫第一個循環:
while (number != zahl || restart.equalsIgnoreCase("j")) { ... }
請注意,您將需要更改邏輯以重新確定新的隨機數。 或者,可以通過創建允許您執行的游戲類來完成重新啟動。 例如這樣的:
public class Zufallszahl {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Game g = new Game();
Random rn = new Random();
while(true) {
g.start(rn.nextInt(100 -1) + 1, s);
System.out.println("Noch ne Runde? Y/N");
String response = s.next();
if(response.equalsIgnoreCase("n")) return;
}
}
public static class Game {
public void start(final int numberToGuess, final Scanner s) {
int number = 0;
int a = 0;
while (number != numberToGuess ) {
a++;
System.out.print("Rate eine Zahl zwischen 0 und 100: ");
number = s.nextInt();
if (number == numberToGuess) {
System.out.println("richtig");
System.out.println("Du hast " + a + " Versuche gebraucht!");
return;
}
if (number < numberToGuess) {
System.out.println("zu klein");
}
if (number > numberToGuess) {
System.out.println("zu gross");
}
}
}
}
}
問候,
嘿,您的代碼可以工作,但是您應該考慮開始編碼更多面向對象的對象,如下所示:
public class Filesplitter {
/**
* @param args the command line arguments
*/
public static int eingabe;
public static int rnd;
public static Scanner sc = new Scanner(System.in);
public static Random rn = new Random();
public static int counter = 0;
public static boolean a = true;
public static void main(String[] args) throws IOException {
while (a = true){
eingabe = 0;
rnd = rn.nextInt(100 - 1)+1;
while (!zahlCheck()){
eingabezahl();
}
System.out.print("möchten sie noch mal spielen ? j/n: ");
if(sc.next().equals("j")){
a = true;
}
else {
a = false;
}
}
}
public static boolean zahlCheck(){
if(eingabe == 0){
}
else if(eingabe != rnd){
if(eingabe < rnd)
System.out.println("Die zahl ist zu klein");
else
System.out.println("Die zahl ist zu groß");
return false;
}else {
System.out.println("Richtig !\n Du hast "+counter+" Versuche gebraucht.");
return true;
}
return false;
}
public static int eingabezahl(){
System.out.println("Bitte geben sie eine Zahl ein:");
eingabe = sc.nextInt();
counter +=1;
return 1;
}
}
這將使事情變得更容易,因為您可以在循環中調用方法;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.