【问题标题】:want to change ASCII while to do~while and for想要更改 ASCII while to do~while 和 for
【发布时间】:2018-09-18 22:53:09
【问题描述】:
import java.util.Scanner;

public class ASCII {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);

        String s;
        char c;

        while(true){
            System.out.println("insert letters. (insert # exit)");
            s = scan.next();
            c = s.charAt(0);
            if(c == '#')
                break;
            System.out.print(c + " ASCII code " + (int)c + "end");
        }
        scan.close();
    }
}

我想改变while to do~while 并为。 我试过了,但我不知道如何更改 if 部分。

【问题讨论】:

  • 你想把它改成do-while还是while-to?您是否在 Google 上搜索过如何在 Java 中使用它们?
  • 您使用的模式对我来说似乎很合适。为什么要改呢?
  • 我正在学习改变它,但如果部分让我很难过
  • 请告诉我们您是如何尝试更改代码的

标签: java


【解决方案1】:

您的问题并不完全清楚,但也许这就是您的想法:

public class Program {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        String s;
        char c;
        do {
            System.out.println("insert letters. (insert # exit)");
            s = scan.next();
            c = s.charAt(0);
            System.out.print(c + " ASCII code " + (int)c + " end \n");
        } while(c != '#');
        scan.close();
    }
}

【讨论】:

    【解决方案2】:

    Java 中有 Do-while 循环:

    do{
                System.out.println("insert letters. (insert # exit)");
                s = scan.next();
                c = s.charAt(0);
                if(c == '#')
                    break;
                System.out.print(c + " ASCII code " + (int)c + "end");
            }while(true);
    

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 2011-07-19
      相关资源
      最近更新 更多