【问题标题】:Doing a change program for my java class, not sure how to loop the whole program?为我的 java 类做一个更改程序,不知道如何循环整个程序?
【发布时间】:2014-09-30 00:38:25
【问题描述】:

目前我正在开发一个程序,在该程序中用户输入一个以美分为单位的值(因此 1.25 美元将是 125),该程序会确定以最少的金额提供多少硬币。我有这个工作。

困扰我的是,我的教授希望程序不断循环,直到用户输入小于 0 的值。我不知道该怎么做,因为每次我尝试时,它只会循环一次,而不是显示适当数量的硬币。

请帮忙。

这是我的代码:

import java.util.Scanner;

public class MakingChange {

    public static void main(String[] args) { 
        //Prompts user to input the change they recieved.
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the amount of change you recieved in coin format. Example: $1.25     would be entered as 125."); 
        int Change = sc.nextInt(); //The value is then stored as an integer named Change.

        int Pennies = 0;
        int Nickels = 0;
        int Dimes = 0;
        int Quarters = 0;

        while (Change < 1){
            System.out.println("Error: Cannot enter a value less than 1!");
            //System.exit(0); //found at http://codingforums.com/java-jsp/69296-%5Bjava%5D-how-end-program.html
        }

        while (Change > 0){ //Runs a loop which determines how many of each coin is used by subtracting the values of the largest first and continuing until 0.
            if (Change >= 25){
                Change -= 25;
                Quarters++;
            }
            else if (Change >= 10){
                Change -= 10;
                Dimes++;
            }
            else if (Change >= 5){
                Change -=5;
                Dimes++;
            }
            else if (Change >= 1){
                Change -= 1;
                Pennies++;
            }


        }
        System.out.println("In total, you should have recieved:");
        System.out.printf("Number of Quarters: %3d  %n", Quarters);
        System.out.printf("Number of Dimes: %6d  %n", Dimes);
        System.out.printf("Number of Nickels: %4d  %n", Nickels);
        System.out.printf("Number of Pennies: %4d  %n", Pennies);
        //Prints out final number of coins used by type of coin.
    }

}

【问题讨论】:

    标签: java loops if-statement while-loop java.util.scanner


    【解决方案1】:

    废弃你的第一个 while 循环。您不希望两个循环处理同一件事。接下来,初始化您的“更改”变量,但将sc.nextInt(); 移动到内部,以便每次迭代都获取输入。最后,将循环设为 do-while 循环,使其至少运行一次(或者您可以将更改初始化为 1)。

    int Change = 0;
    do{
      Change = sc.nextInt();
      // ...
    }
    while(Change > 0);
    

    【讨论】:

      【解决方案2】:

      您需要询问用户他们在循环中获得了多少变化。下面是一些代码:

      import java.util.scanner;
      public void MakingChange {
      public static void main(String[] args) {
      int change = 0;
      int currentchange = 0;
      Scanner in = new Scanner(System.in);
      do {
      change += currentchange;
      System.out.println("Enter an amount of change.");
      currentchange = in.nextInt();
      } while(currentchange > 0);
      }
      }
      System.out.println("Total change: " + change);
      

      +1 To Gary for do 循环的想法。 你明白了。

      【讨论】:

        【解决方案3】:

        我在您的代码中发现了一些问题,Java 命名约定的变量以小写字母开头。接下来,您可以简单地循环 Scanner.hasNextInt()。然后,如果您在自己的循环中计算硬币、硬币和镍币(您的镍币计数器计为硬币),您的代码似乎会更简单。所以,像 -

        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String msg = "Please enter the amount of change "
                    + "you received in coin format. Example: "
                    + "$1.25 would be entered as 125.";
            System.out.println(msg);
            while (sc.hasNextInt()) {
                int change = sc.nextInt();
                int pennies = 0;
                int nickels = 0;
                int dimes = 0;
                int quarters = 0;
        
                if (change < 1) {
                    System.out.println("Error: Cannot enter a value less than 1!");
                } else {
                    while (change >= 25) {
                        change -= 25;
                        quarters++;
                    }
                    while (change >= 10) {
                        change -= 10;
                        dimes++;
                    }
                    while (change >= 5) {
                        change -= 5;
                        nickels++;
                    }
                    pennies = change;
                }
                System.out.println("In total, you should have recieved:");
                System.out.printf("Number of Quarters: %3d  %n", quarters);
                System.out.printf("Number of Dimes: %6d  %n", dimes);
                System.out.printf("Number of Nickels: %4d  %n", nickels);
                System.out.printf("Number of Pennies: %4d  %n", pennies);
                System.out.println(msg);
            }
        }
        

        【讨论】:

          【解决方案4】:

          我会像这样简化你的循环:

          int change;
          while ((change = sc.nextInt()) >= 0) {
              //do code
          }
          

          由于 Java 命名约定,请注意小写 change

          您的代码原样的主要问题是您有两个循环,如果您的数字不小于 1,您将立即退出第一个循环(如果小于 1,则无限循环)。你做了一次验证,然后做了你的程序逻辑。尝试使用单个循环。

          【讨论】:

            【解决方案5】:

            您需要围绕所有内容进行循环。循环顶部会提示并读取值,循环底部会打印输出。但是因为是循环,所以会返回并再次提示。在给定的代码中,您没有该级别的循环。

            【讨论】:

              【解决方案6】:
              import java.util.Scanner;
              
              public class MakingChange {
              
              public static void main(String[] args) { 
                  //Prompts user to input the change they recieved.
                  Scanner sc = new Scanner(System.in);
              
                  int input = 0;
                  do{
                      System.out.println("Please enter the amount of change you recieved in coin format. Example: $1.25     would be entered as 125."); 
                      input= sc.nextInt(); //The value is then stored as an integer named Change.
                      if(input<=0) {
                       System.out.println("Good Bye");
                       break; breaks the loop
                      }
                  else{
                      Change=input;
                      int Pennies = 0;
                      int Nickels = 0;
                      int Dimes = 0;
                      int Quarters = 0;
              
                      while (Change > 0){ //Runs a loop which determines how many of each coin is used by subtracting the values of the largest first and continuing until 0.
                          if (Change >= 25){
                               Change -= 25;
                              Quarters++;
                         }
                        else if (Change >= 10){
                          Change -= 10;
                          Dimes++;
                        }
                        else if (Change >= 5){
                          Change -=5;
                          Dimes++;
                        }
                        else if (Change >= 1){
                          Change -= 1;
                          Pennies++;
                        }
                     }
                     System.out.println("In total, you should have recieved:");
                     System.out.printf("Number of Quarters: %3d  %n", Quarters);
                     System.out.printf("Number of Dimes: %6d  %n", Dimes);
                     System.out.printf("Number of Nickels: %4d  %n", Nickels);
                     System.out.printf("Number of Pennies: %4d  %n", Pennies);
                  //Prints out final number of coins used by type of coin.
                 }//end of else
                }while(input>0);//end of do while
              
              }
              

              【讨论】:

                【解决方案7】:

                只需创建一个 while 循环并一直引用它直到输入

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2021-08-12
                  • 2019-07-05
                  • 2018-09-29
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-12-26
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多