Java中的字符常量无效?

Java中的字符常量无效?,java,Java,这是我的密码: import java.util.Scanner; import javax.swing.JOptionPane; import java.text.DecimalFormat; /* Medium Speed Air 1100 feet per second Water 4900 feet per second Steel 16,400 feet per second Write a program that asks the user to enter "air", "

这是我的密码:

import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

/*

Medium Speed
Air 1100 feet per second
Water 4900 feet per second
Steel 16,400 feet per second

Write a program that asks the user to enter "air", "water", or "steel", and the distance that a sound wave will
travel in the medium. The program should then display the amount of time it will take.
You can calculate the amount of time it takes sound to travel in air with the following formula:

Time = Distance / 1100
You can calculate the amount of time it takes sound to travel in water with the following formula:

Time = Distance / 4900
You can calculate the amount of time it takes sound to travel in steel with the following formula:

Time = Distance / 16400
*/

public class SpeedOfSound
{
    public static void main(String[] args)
      {


        String input;   
        char timeTraveled;

        Scanner keyboard = new Scanner(System.in);


        double distance;
        double time;
        double time2;
        double time3;
        time = (distance/ 1100);
        time2 = (distance/ 4900);
        time3 = (distance/ 16400);
        DecimalFormat formatter = new DecimalFormat("#0.00");



                System.out.println("Enter air, water, or steel: ");
                input = keyboard.nextLine();
                System.out.print("Enter distance: ");
                distance = keyboard.nextDouble();



               switch(timeTraveled)
               {
                case 'air':

                System.out.printf("The total time traveled is " + formatter.format(time) + ".");
                break;

                case "water":


                System.out.printf("The total time traveled is " + formatter.format(time2) + ".");
                break;

                case "steel":


                System.out.printf("The total time traveled is " + formatter.format(time3) + "seconds.");
                timeTraveled = input.charAt(0);
                break;
                keyboard.close();
    }
} // main()
}  // class SpeedOfSound

为什么
case'air':
两次给我错误
无效字符常量?我的教授对不同的程序有不同的例子,这和我正在做的几乎一样,但他没有得到错误。为什么会出现此错误?

在某些编程语言中,单引号(
)和双引号(
)是可互换的。在Java中(以及在C和C++中),它们不是可互换的

如果要指定多字符串文字,请使用双引号:
“air”


此外,当您将
字符(
timeTraveled
)与字符串(
“air”
)进行比较时,还不清楚会发生什么情况。

在某些编程语言中,单引号(
)和双引号(
)是可互换的。在Java(以及C和C++)中,它们不是

如果要指定多字符串文字,请使用双引号:
“air”


此外,当您将
字符(
timeTraveled
)与字符串(
“air”
)进行比较时,您不清楚会发生什么情况。

在某些编程语言中,单引号(
)和双引号(
)是可互换的。在Java中(以及在C和C++中),它们不是可互换的

如果要指定多字符串文字,请使用双引号:
“air”


此外,当您将
字符(
timeTraveled
)与字符串(
“air”
)进行比较时,还不清楚会发生什么情况。

在某些编程语言中,单引号(
)和双引号(
)是可互换的。在Java(以及C和C++)中,它们不是

如果要指定多字符串文字,请使用双引号:
“air”


此外,当您将
char
timeTraveled
)与字符串(
“air”
)进行比较时,还不清楚会发生什么情况。

“air”
使用单引号。单引号表示字符常量。您要查找的是
“air”
,一个字符串常量

你好像是个新程序员。我对您的程序做了一些改进,我将在这里向您展示:

import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

/*
Medium Speed
Air 1100 feet per second
Water 4900 feet per second
Steel 16,400 feet per second

Write a program that asks the user to enter "air", "water", or "steel", and the distance that a sound wave will
travel in the medium. The program should then display the amount of time it will take.
You can calculate the amount of time it takes sound to travel in air with the following formula:

Time = Distance / 1100
You can calculate the amount of time it takes sound to travel in water with the following formula:

Time = Distance / 4900
You can calculate the amount of time it takes sound to travel in steel with the following formula:

Time = Distance / 16400
*/

public class SpeedOfSound {
        public static void main(String[] args) {

        char timeTraveled; //what is this even doing here?

        Scanner scanner = new Scanner(System.in);

        double time = (distance/ 1100);
        double time2 = (distance/ 4900);
        double time3 = (distance/ 16400);
        DecimalFormat formatter = new DecimalFormat("#0.00");

                System.out.println("Enter air, water, or steel: ");
                String material = scanner.nextLine();
                System.out.print("Enter distance: ");
                double distance = scanner.nextDouble();

                switch (material) {
                    case "air":
                        System.out.printf("The total time traveled is " + formatter.format(time) + ".");
                        break;

                    case "water":
                        System.out.printf("The total time traveled is " + formatter.format(time2) + ".");
                        break;

                    case "steel":
                        System.out.printf("The total time traveled is " + formatter.format(time3) + "seconds.");
                        timeTraveled = material.charAt(0); //what is this even doing here?
                    break;
                }
                scanner.close();
    } // main()
}  // class SpeedOfSound
  • 使间距和缩进更加一致
  • 重命名扫描仪对象。“键盘”不是扫描仪对象的合适名称,因为扫描仪不仅可以使用键盘输入,还可以使用字符串和文件输入
  • 我结合了“时间”变量的声明和定义
例如

  • “air”
    更改为
    “air”
    ,并将开关大小写变量更改为“material”(过去称为“input”,是保存用户输入的字符串),而不是使用时间旅行(一些杂项字符?)
由于您的程序只显示三种可能性中的一种,为什么要计算所有三种可能性?我建议您按如下方式重新编写算法: 询问用户所需的材料和距离。根据用户选择的空气、水或钢,将可变“速度”设置为1100、4900或16400。然后,将时间计算为距离/速度


这样可以避免重复3个相同的
System.out.println()
语句,避免出现3个时间变量(当您只需要1个时),

使用单引号。单引号表示字符常量。您要查找的是
,一个字符串常量

你好像是个新程序员。我对您的程序做了一些改进,我将在这里向您展示:

import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

/*
Medium Speed
Air 1100 feet per second
Water 4900 feet per second
Steel 16,400 feet per second

Write a program that asks the user to enter "air", "water", or "steel", and the distance that a sound wave will
travel in the medium. The program should then display the amount of time it will take.
You can calculate the amount of time it takes sound to travel in air with the following formula:

Time = Distance / 1100
You can calculate the amount of time it takes sound to travel in water with the following formula:

Time = Distance / 4900
You can calculate the amount of time it takes sound to travel in steel with the following formula:

Time = Distance / 16400
*/

public class SpeedOfSound {
        public static void main(String[] args) {

        char timeTraveled; //what is this even doing here?

        Scanner scanner = new Scanner(System.in);

        double time = (distance/ 1100);
        double time2 = (distance/ 4900);
        double time3 = (distance/ 16400);
        DecimalFormat formatter = new DecimalFormat("#0.00");

                System.out.println("Enter air, water, or steel: ");
                String material = scanner.nextLine();
                System.out.print("Enter distance: ");
                double distance = scanner.nextDouble();

                switch (material) {
                    case "air":
                        System.out.printf("The total time traveled is " + formatter.format(time) + ".");
                        break;

                    case "water":
                        System.out.printf("The total time traveled is " + formatter.format(time2) + ".");
                        break;

                    case "steel":
                        System.out.printf("The total time traveled is " + formatter.format(time3) + "seconds.");
                        timeTraveled = material.charAt(0); //what is this even doing here?
                    break;
                }
                scanner.close();
    } // main()
}  // class SpeedOfSound
  • 使间距和缩进更加一致
  • 重命名扫描仪对象。“键盘”不是扫描仪对象的合适名称,因为扫描仪不仅可以使用键盘输入,还可以使用字符串和文件输入
  • 我结合了“时间”变量的声明和定义
例如

  • “air”
    更改为
    “air”
    ,并将开关大小写变量更改为“material”(过去称为“input”,是保存用户输入的字符串),而不是使用时间旅行(一些杂项字符?)
由于您的程序只显示三种可能性中的一种,为什么要计算所有三种可能性?我建议您按如下方式重新编写算法: 询问用户所需的材料和距离。根据用户选择的空气、水或钢,将可变“速度”设置为1100、4900或16400。然后,将时间计算为距离/速度


这样可以避免重复3个相同的
System.out.println()
语句,避免出现3个时间变量(当您只需要1个时),

使用单引号。单引号表示字符常量。您要查找的是
,一个字符串常量

你好像是个新程序员。我对您的程序做了一些改进,我将在这里向您展示:

import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

/*
Medium Speed
Air 1100 feet per second
Water 4900 feet per second
Steel 16,400 feet per second

Write a program that asks the user to enter "air", "water", or "steel", and the distance that a sound wave will
travel in the medium. The program should then display the amount of time it will take.
You can calculate the amount of time it takes sound to travel in air with the following formula:

Time = Distance / 1100
You can calculate the amount of time it takes sound to travel in water with the following formula:

Time = Distance / 4900
You can calculate the amount of time it takes sound to travel in steel with the following formula:

Time = Distance / 16400
*/

public class SpeedOfSound {
        public static void main(String[] args) {

        char timeTraveled; //what is this even doing here?

        Scanner scanner = new Scanner(System.in);

        double time = (distance/ 1100);
        double time2 = (distance/ 4900);
        double time3 = (distance/ 16400);
        DecimalFormat formatter = new DecimalFormat("#0.00");

                System.out.println("Enter air, water, or steel: ");
                String material = scanner.nextLine();
                System.out.print("Enter distance: ");
                double distance = scanner.nextDouble();

                switch (material) {
                    case "air":
                        System.out.printf("The total time traveled is " + formatter.format(time) + ".");
                        break;

                    case "water":
                        System.out.printf("The total time traveled is " + formatter.format(time2) + ".");
                        break;

                    case "steel":
                        System.out.printf("The total time traveled is " + formatter.format(time3) + "seconds.");
                        timeTraveled = material.charAt(0); //what is this even doing here?
                    break;
                }
                scanner.close();
    } // main()
}  // class SpeedOfSound
  • 使间距和缩进更加一致
  • 重命名扫描仪对象。“键盘”不是扫描仪对象的合适名称,因为扫描仪不仅可以使用键盘输入,还可以使用字符串和文件输入
  • 我结合了“时间”变量的声明和定义
例如

  • “air”
    更改为
    “air”
    ,并将开关大小写变量更改为“material”(过去称为“input”,是保存用户输入的字符串),而不是使用时间旅行(一些杂项字符?)
既然你的程序只会显示三种可能性中的一种,为什么要计算所有的三种呢?我建议你修改你的算法
String timeTraveled;
if (timeTraveled.equals("air")){
  //do something
} else if (timeTraveled.equals("water")) {
  //do something
} ...