2014-10-03 26 views
0

這裏是我的代碼: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兩次?我的教授對另一個程序有不同的例子,它和我正在做的幾乎一樣,但他沒有得到錯誤。爲什麼我會得到這個錯誤?

+1

空氣不是一個char – 2014-10-03 06:02:09

+0

嘿傢伙感謝您的快速反應,我就修理好惹的timeTraveled,如我正在按照我的教授的例子,忘記擦除那部分。 (擦除它不會做任何事情)。另外,當我在空氣水和鋼周圍加上引號時,它給了我錯誤「類型不匹配,無法將字符串轉換爲字符」 – justaregularguy 2014-10-03 06:07:08

+0

您正在嘗試使用混合類型切換(char)timeTraveled。字符和字符串。你不能那樣做。聲明timeTraveled以輸入String並用雙引號將空氣換行。 – 2014-10-03 06:26:10

回答

0

在某些編程語言中,單引號(')和雙引號(")是可互換的。在Java(以及C和C++)中,它們不是。

如果要指定多字符字符串文字,請使用double quotes:"air"

此外,還不清楚在將chartimeTraveled)與字符串("air")進行比較時會發生什麼。

+0

但char timeTraveled被聲明爲char。 – 2014-10-03 06:03:12

+0

@getlost swtich大小寫不接受字符:) – 2014-10-03 06:04:25

+1

@KickButtowski你是什麼意思..他接受聊天。只接受原語。不是對象 – 2014-10-03 06:07:24

-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 
  • 作出的間距和縮進更一致
  • 更名爲您的掃描儀的對象。 「鍵盤」不是掃描儀對象的合適名稱,因爲掃描儀不僅適用於鍵盤輸入,還適用於字符串和文件輸入。
  • 我結合聲明你的 「時間」 變量定義

例如

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"」,另外,改變了開關殼體變量設置爲‘材料’(其過去被稱爲‘輸入’,並且是保持用戶的輸入字符串),而不是將其?使用timeTraveled(?一些其他字符)

因爲你的程序是唯一將要顯示的三種可能有一段時間,爲什麼計算所有3,我建議你返工你的算法如下: 詢問用戶的材料和距離他們想要設置一個變量「速度」相等到1100,4900或16400,取決於用戶對空氣,水或鋼的選擇。然後,計算時間作爲距離/速度。

這樣可以節省你從重複3條相同System.out.println()語句,把你從3個時間變量(當你只需要1),

3

您在這裏有幾個問題。

首先,單引號是爲單個字符保留的,如'a'。整個字符串需要放在雙引號中。

其次,timeTraveled在您使用它之前從未被分配過任何東西,所以當您試圖運行它(並且編譯時)時,它可能沒有被初始化。您可能需要使用input

這就是說,只要你使用的是Java 7或更高版本,你應該寫爲您的開關參數:

switch(input) { 
    // statements to follow 
} 

我不知道這是什麼任務,在結束你的"steel"大小寫意味着這樣做,但您可能需要將其邏輯完全從switch聲明中移出。

0

我找到了多個問題,在您的代碼:

  1. 應該"air"'air'(您OP解決方案)。
  2. timeTraveled的數據類型是char,但您試圖將它與String匹配(如「air」,「water」等)。
  3. timeTraveled未初始化。
  4. distance在爲time,time1 & time2進行計算時未初始化。
  5. keyboard.close();是無法訪問的代碼。將其移動到switch區塊外或將其添加到default的情況下。

理想情況下,您應該在開關盒中使用字符或創建enum以獲得更好的清晰度。

+0

空氣水不能聲明爲字符.op應該肯定使用枚舉 – 2014-10-03 06:14:34

1

我不明白這個程序的邏輯。如果ü需要輸入單詞,然後做取決於它的東西儘量讓類似

String timeTraveled; 
if (timeTraveled.equals("air")){ 
    //do something 
} else if (timeTraveled.equals("water")) { 
    //do something 
} ... 
+0

這是最簡單的方法比去枚舉 – 2014-10-03 06:15:34

+0

隨着Java 7,這兩個表達式是等價的(除了'string'的大寫,因爲這是Java而不是C#)。 – Makoto 2014-10-03 06:17:22

+0

如果在'switch'很適合的情況下不要使用'if' /'else if' /'else' – Alexander 2014-10-03 06:18:55