【问题标题】:Running loop on Java method在 Java 方法上运行循环
【发布时间】:2015-12-03 12:43:52
【问题描述】:

所以我有这个关于做一些实现方法的课程的作业。我做了这个非常简单的程序来给你一天的幸运数字。但由于某种原因,我无法让它正常运行两次以上。

我发布的版本不包含循环,但我尝试了大约 10 种不同的方法,但它不起作用。要么它会不停地吐出数字,要么它会再次打印出欢迎行,而不仅仅是“你想要另一个数字是/否”行。如果有人能帮我弄清楚我应该如何组织它,以便循环只显示这一行:

Would you like to receive another number? y/n

然后如果用户决定是,则 intro 方法再次运行,并且该行再次显示,直到 uses 按下“n”

代码如下:

import java.util.Scanner;
import java.math.BigInteger; 
import java.util.Random; 

public class MinOppgave2 {

public static void main(String[] args) {
    menu();
}

    public static void menu(){
    //intro text
    System.out.println("Welcome to lucky number of the day!");
    System.out.println("What kind of number would you like today?");
    intro();
    Scanner input = new Scanner(System.in);
    System.out.println("Would you like to receive another number? y/n");
    String txtinput = input.nextLine();
                if (txtinput.equalsIgnoreCase("y")){
            intro();
                }
            else if (txtinput.equalsIgnoreCase("n")){
                System.out.println("That's all for now, have a nice day!");
            }
    System.out.println("That's all for now, have a nice day!");
    }

    public static void intro(){
    // user choice
    Scanner input = new Scanner(System.in);
    System.out.println("Please choose between: even odd or prime");
    String text1 = input.nextLine();
    //if/else user choice arguments
    if (text1.equalsIgnoreCase("even"))
        Evennum();  
        else if (text1.equalsIgnoreCase("odd"))
            Oddnum();
        else if (text1.equalsIgnoreCase("prime"))
            Prime();
        else
            menu();
    }

    public static void Evennum(){
        // random number generator
        int num = 0;
        Random rand = new Random();
        num = rand.nextInt(1000) + 1;
        while (!isEven(num)) {          
            num = rand.nextInt(1000) + 1;
        }
        System.out.println(num);
    }

    public static void Oddnum(){
        // random number generator
        int num = 0;
        Random rand = new Random();
        num = rand.nextInt(1000) + 1;

        while (!isOdd(num)) {          
            num = rand.nextInt(1000) + 1;
        }
        System.out.println(num);
    }

    public static void Prime(){
        // random number generator
        int num = 0;
        Random rand = new Random();
        num = rand.nextInt(1000) + 1;
        while (!isPrime(num)) {          
            num = rand.nextInt(1000) + 1;
        }
        System.out.println(num); 
    }


    // prime checker
    private static boolean isPrime(int numin){
        if (numin <= 3 || numin % 2 == 0) 
            return numin == 2 || numin == 3;
        int divisor = 3;
        while ((divisor <= Math.sqrt(numin)) && (numin % divisor != 0)) 
            divisor += 2;
        //true/false prime answer
        return numin % divisor != 0;
    }

    private static boolean isEven(int numin){
        //math argument for even number
        return (numin % 2) == 0;
    }

    private static boolean isOdd(int numin){
        //math argument for even number
        return (numin % 2) == 1;
    }
}

【问题讨论】:

    标签: java loops numbers boolean calculator


    【解决方案1】:

    在错误的地方进行错误的递归......

    试试这个:

    public static void intro() {
      System.out.println("Welcome to lucky number of the day!");
      System.out.println("What kind of number would you like today?");
    }
    
    public static String takeInput() {
      Scanner input = new Scanner(System.in);
      return input.nextLine();
    }
    
    public static boolean pickNumber() {
      System.out.println("Would you like to receive another number? y/n");
      if (!takeInput().equalsIgnoreCase("y")) {
        System.out.println("That's all for now, have a nice day!");
        return false;
      }
    
      System.out.println("Please choose between: even odd or prime");
      String chosen = takeInput();
      //if/else user choice arguments
      if (chosen.equalsIgnoreCase("even"))
        Evennum();
      else
        if (chosen.equalsIgnoreCase("odd"))
          Oddnum();
        else
          if (chosen.equalsIgnoreCase("prime"))
            Prime();
    
      return true;
    }
    
    public static void main(String[] args) {
      intro();
      while (pickNumber())
        ;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 2014-04-13
      • 2011-12-10
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      相关资源
      最近更新 更多