倒计时和打印循环时遇到问题

时间:2016-04-17 03:16:21

标签: java for-loop increment

所以我需要我的代码以3个用户输入的增量打印出来,我有第二个java文件使用点符号来执行方法。所以它应该像这样运行。

选择>起始值 - 选择增量值 - 选择>结束价值 所有这些都是用户输入,如果起始值>我需要它。然后结束然后从用户输入增量的起始值开始计数。但如果结束

import java.util.Scanner;

public class logic  {

    public static void main(String [] args) {

    //new scanner
    Scanner input = new Scanner(System.in);

    //Data
      char ch = 0;
      int start = 0; 
      int end = 0;
      int inc = 0;
      String printStr = "";
      final int SENTINEL = -1;
      String menu ="Looping (Demo)" +
                   "\nStart Value\t [S]" +
                   "\nIncrement value  [I]" +
                   "\nEnd Value\t [E]" + 
                   "\nFor Looping\t [F]" +
                   "\nQuit\t\t [Q]" +
                   "\nEnter Option > "; 

    while(ch != SENTINEL) {

    switch(ch) {
        case 'S':
        case 's':

            start = UtilsDM.readInt("Enter loop start value: ", false);      
            break;

        case 'I':
        case 'i':        
            inc = UtilsDM.readInt("Enter loop increment value: ", false);          
            break;   

        case 'E':
        case 'e':
            end = UtilsDM.readInt("Enter loops end value: ", false);
            break;      

        case 'F':
        case 'f': 
            if(start <= end){
                for (int i=start; i<=end; i+=inc) {                     
                    System.out.print(i + " ");
                }//end loop +
            }//end if
            else if(start >= end){ 
                for (int i=end; i<=start; i-=inc) {                
                    System.out.print(i + " ");                    
                }//end loop -
            }//end else if

            System.out.println("\n");                  
            break; 

        case 'Q':
        case 'q':
            System.out.println("Terminating upon user command.");
            System.exit(0);
            break; 

        default:
            System.out.println("Unrecognized character");
            break;
    }//end switch

    ch =  UtilsDM.readChar(menu, false);
   }//end loop







    //computations, algorithms

    //outputs, formatting, display
    } //end main  

}//end class

1 个答案:

答案 0 :(得分:2)

更改案例&#39; F&#39;如下,我评论了这些变化: -

>>=