【问题标题】:How can I use leniently increasing numbers as one of my conditions?我怎样才能使用宽大的数字作为我的条件之一?
【发布时间】:2016-11-30 18:40:27
【问题描述】:

我必须决定我的输入是按升序还是降序排列。 例如,如果是1234,它会说Increasing! 或者8765Decreasing! 我还需要添加浅增加和减少。

像这样:

  • 344343 => Shallow increase!
  • 443343 => Shallow decrease!
  • 444 => either/or!

示例代码:

 public class Order {
 public static void main(String[] args) {
    boolean stop = false;

         while(!stop){
         Scanner scanner = new Scanner(System.in);
         System.out.println("Do you wanna continue? Y or N? ");
         char c = scanner.next().charAt(0);
         c = Character.toUpperCase(c);

            if(c =='Y'){
            System.out.println("Please enter a number: "); 
        Scanner n = new Scanner(System.in);
            int s = n.nextInt();
            boolean increasing = true;

               while ( s> 0) {
              int d1 = s % 10;
              s/=10;
              int d2 = s % 10;

                  if(d2>d1){
            increasing = false;
            System.out.println("decreasing!");
            break;
              }
              else{
            System.out.println("increasing!");
            break;
              }
           }
        }
        else {
       System.out.println("haha..K ");
               stop= true;
        }
     }
  }
}

我想要的输出:

1234  Increasing!  
8765  is Decreasing! 
344   Shallow increase! 
443   Shallow decrease !
444   either/or!

【问题讨论】:

  • 因此,这也将被视为非此即彼,对吧? 44444、12321 或 12121 将处于相同的状态。你认为我是对的吗?
  • 我不知道。您的需求来自哪里?
  • 我很高兴你回来了!
  • 我是学生..在我的练习中...
  • 你需要真正说出“浅增/减低”是什么意思。

标签: java sorting input numbers digits


【解决方案1】:

您可以尝试将 (first = second && second >= third ||third >= first) 用于浅减少.另外,测试它们是否都相等,然后输入/或。

【讨论】:

  • 你在做 Java For Everyone 的书吗,因为我也在做同样的事情 :)
猜你喜欢
  • 1970-01-01
  • 2020-06-07
  • 2010-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
相关资源
最近更新 更多