[英]How can I repeat a public class in Java?
I looked for a long time for a possibility to repeat a public class in Java, I found a solution with continue and repeating an additional while loop. 我花了很长时间寻找在Java中重复一个公共类的可能性,我找到了一种方法,可以继续并重复另一个while循环。 But I am really not happy with it, is there a simpler method do to this?
但是我真的不满意,有没有更简单的方法可以做到这一点? Thanks for the help guys.
感谢您的帮助。
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;
}
}
}
}
About restarting - that seems odd unless you are extracting it in an object. 关于重新启动-除非您将其提取到对象中,否则这似乎很奇怪。 So solution 1 may be to just run in a loop and get rid of the first one, by checking the restart in the second:
因此,解决方案1可能是只是循环运行,并通过检查第二个循环中的重新启动来摆脱第一个循环:
while (number != zahl || restart.equalsIgnoreCase("j")) { ... }
Note that you will need to change your logic to reassing a new random number. 请注意,您将需要更改逻辑以重新确定新的随机数。 Alternatively, restarting could be done by creating a game class that allows you to execute it.
或者,可以通过创建允许您执行的游戏类来完成重新启动。 For example smth like this:
例如这样的:
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");
}
}
}
}
}
Regards, 问候,
Hey you code works but you should consider to start codinge more object orientated like this: 嘿,您的代码可以工作,但是您应该考虑开始编码更多面向对象的对象,如下所示:
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;
}
} }
this will make things way easier since you can just call your methode in side your loop ;) 这将使事情变得更容易,因为您可以在循环中调用方法;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.