Java中的字符常量无效?

时间:2014-10-03 06:00:02

标签: 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", "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':两次错误invalid character constant?我的教授对不同的程序有一个不同的例子,它与我正在做的几乎相同,但他没有得到错误。为什么我会收到此错误?

6 个答案:

答案 0 :(得分:3)

你在这里遇到了几个问题。

首先,单引号保留给单个字符,例如'a'。整个字符串需要放在双引号中。

其次,timeTraveled在您使用它的时候从未被分配过任何东西,所以它可能"可能"在你尝试运行它时(并且编译好的东西)没有初始化。您可能希望改为使用input

这就是说,只要你使用Java 7或更新版本,你就应该把它写成你的switch参数:

switch(input) {
    // statements to follow
}

我不确定"steel"案例结尾处的任务是什么意思,但您可能希望完全将其逻辑从switch语句中删除。

答案 1 :(得分:1)

我不明白这个程序的逻辑。如果U需要输入单词然后根据它做某事尝试制作类似

的内容
String timeTraveled;
if (timeTraveled.equals("air")){
  //do something
} else if (timeTraveled.equals("water")) {
  //do something
} ...

答案 2 :(得分:0)

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

如果要指定多字符字符串文字,请使用 double 引号:"air"

此外,当您将chartimeTraveled)与字符串("air")进行比较时,目前尚不清楚会发生什么。

答案 3 :(得分:0)

我在您的代码中发现了多个问题:

  1. 应该"air"而不是'air'(您的操作解决方案)。
  2. timeTraveled的数据类型为char,但您尝试将其与字符串匹配(例如&#34; air&#34;,&#34; water&#34;等)。< / LI>
  3. timeTraveled未初始化。
  4. 在对distancetime&amp;进行计算时,未初始化
  5. time1 time2
  6. keyboard.close();是无法访问的代码。将其移至switch块之外或将其添加到default案例中。
  7. 理想情况下,您应该在切换案例中使用字符或创建enum以获得更清晰的信息。

答案 4 :(得分:0)

@justaregularguy-您收到此错误,因为您已将air当作字符。

提及空气为String,您会没事的。 这将帮助您-如果您尝试使用不允许的值。

“无法打开Float类型的值。仅允许使用可转换的int值,字符串或枚举变量”

答案 5 :(得分:-1)

'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
  • 使间距和缩进更加一致
  • 重命名了您的扫描仪对象。 “keyboard”不是Scanner对象的合适名称,因为扫描仪不仅可以使用键盘输入,还可以使用字符串和文件输入。
  • 我合并了“时间”变量的声明定义

E.g。

double time; //a declaration of "time"
time = (distance/ 1100); //a definition of "time"
//becomes:
double time = (distance/ 1100); //a declaration AND definition of "time"
  • 'air'更改为"air"“,同时将switch case变量更改为”material“(以前称为”input“,并且是保存用户输入的字符串),而不是比使用timeTraveled(一些杂项字符?)

由于你的程序只会显示三种可能性中的一次,为什么要计算所有3种?我建议您按如下方式重新编写算法: 询问用户他们想要的材料和距离。将变量“速度”设置为1100,4900或16400,具体取决于用户对空气,水或钢的选择。然后,将时间计算为距离/速度。

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

相关问题