简体   繁体   English

如何在Java中重复公共课程?

[英]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.

相关问题 我怎样才能只获得java类的受保护的公共构造函数? - How can I get only protected and public constructors of a java class? 如何从 Java 应用程序中的另一个类访问在实用程序类中声明的公共最终静态列表? - How can I access to a public final static list declared into an utility class from another class in a Java application? 如何使用java代理在java中为一个类文件生成多个公共类 - How can I generate multiple public classes for one class file in java using java agent 如何在Java中的Android上重复位图? - How can i repeat bitmap on Android in Java? 如何在 java 中重复 AnimationSet? - How can I repeat an AnimationSet in java? 如何减少 Java 8 中的重复代码? - How can i reduce this repeat code in Java 8? 如何为SimpleCursorAdapter编写公共类? - how can i code public class for SimpleCursorAdapter? 我如何在此类中访问公共数组 - How can I access the public array in this class 如何(自动)确定Java源文件是否包含公共类,如果是,则确定其名称? - How can I (automatically) determine whether a Java source file contains a public class, and if so, its name? 我如何从另一个包中实例化Java内部的public static final类 - How can I instantiate a inner public static final class in java from a different package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM