2014-10-30 86 views
-1

我的程序的目的是接受來自用戶的整數,雙精度和字符串,並且當通過輸入單詞「quit」終止程序時,程序平均整數和雙精度,並輸出提交的字符串。這是我到目前爲止:從掃描儀輸入輸出多個字符串

import java.util.*; 

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

     double sumI = 0; 
     double sumD = 0; 
     String words = ""; 
     int numInputInt = 0; 
     int numInputDoub = 0; 
     do { 
      System.out.print("Enter something: "); 
      if (console.hasNextInt()) { 
       int numI = console.nextInt(); 

      if (numI >= -100 && numI <= 100) { 
       sumI += numI; 
       numInputInt++; 
      } 
      else { 
       System.out.println("Integer out of range!(-100 .. 100)"); 
      } 

      } 
     else if (console.hasNextDouble()) { 
      double numD = console.nextDouble(); 
      if (numD >= -10.0 && numD <= 10.0) { 
       sumD += numD; 
       numInputDoub++; 
      } 
      else { 
       System.out.println("Double out of range!(-10.0 .. 10.0)"); 
      } 

     } 
     else { 
      words = console.next(); 
     } 

     } while (!words.equalsIgnoreCase("quit")); 
     System.out.println("Program terminated..."); 
     double avgInt = sumI/numInputInt; 
     double avgDoub = sumD/numInputDoub; 

     if (numInputInt > 0) { 
     System.out.println("\tAveragae of Integers: " + avgInt); 
     } 
     else { 
     System.out.println("\tNo intergers submitted"); 
     } 
     if (numInputDoub > 0) { 
     System.out.println("\tAverage of Doubles: " + avgDoub); 
     } 
     else { 
     System.out.println("\tNo doubles submitted"); 
     } 
     System.out.println(words); 


    } 

} 

整數和雙打得到處理好,但我卡在字符串。有關如何去做的任何想法? 在此先感謝!

回答

0

你可以使用

String input = ""; 
do { 
    //... 
    } 
    else { 
     input = console.next();    
     words += input + " "; 
    } 
    console.nextLine(); // Read the carriage return 
} while (!input.equalsIgnoreCase("quit")); 
//... 
System.out.println(words.trim()); 

來串聯文本,但是當一個循環中完成的,這是相當低效。

一個更好的解決辦法是使用StringBuilder ......

StringBuilder work = new StringBuilder(128); 
String input = ""; 
do { 
    //... 
    } 
    else { 
     input = console.next();    
     words.append(" ").append(input); 
    } 
    console.nextLine(); // Read the carriage return 
} while (!input.equalsIgnoreCase("quit")); 
//... 
System.out.println(words); 
+0

謝謝,但我不能使用任何其他類除了我已經使用 – GPC 2014-10-30 01:48:38

+0

的那些'StringBuilder'大約是有效的內循環,因爲你不是創建臨時'字符串'的「大量」,否則,第一個例子應該讓你開始... – MadProgrammer 2014-10-30 01:56:44

+0

此外,我不會使用'單詞'來檢查退出條件,它只會永遠是真的,如果用戶輸入「退出」的第一個字... – MadProgrammer 2014-10-30 01:57:30

0

雖然你可以建立單詞的List<String>看起來你只是想每一個新詞串聯到您的String words一樣,

if (words.length() > 0) words += " "; // <-- add a space. 
words += console.next(); // <-- += not = 

然後你的循環測試更改爲類似,

while (!words.trim().toLowerCase().endsWith("quit")); 

它應該像你期望的那樣工作。

+0

te字符串「退出」的情況並不重要,我不需要它在循環測試權嗎? – GPC 2014-10-30 01:52:06

+0

定義*無關緊要*。如果你的意思是它總是小寫,那麼你可以跳過'toLowerCase()' – 2014-10-30 01:53:01

+0

使用你的方法我得到了一個無限循環 – GPC 2014-10-30 01:56:41

-1

我剛剛看了你的意見,你不能使用任何其它類,試試這個:

import java.util.*; 

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

     double sumI = 0; 
     double sumD = 0; 
     String words = ""; 
     int numInputInt = 0; 
     int numInputDoub = 0; 
     do { 
      System.out.print("Enter something: "); 
      if (console.hasNextInt()) { 
       int numI = console.nextInt(); 

      if (numI >= -100 && numI <= 100) { 
       sumI += numI; 
       numInputInt++; 
      } 
      else { 
       System.out.println("Integer out of range!(-100 .. 100)"); 
      } 

      } 
     else if (console.hasNextDouble()) { 
      double numD = console.nextDouble(); 
      if (numD >= -10.0 && numD <= 10.0) { 
       sumD += numD; 
       numInputDoub++; 
      } 
      else { 
       System.out.println("Double out of range!(-10.0 .. 10.0)"); 
      } 

     } 
     else { 
      words = words.concat(" ").concat(console.next()); 

     } 

     } while (!words.contains("quit")); 
     System.out.println("Program terminated..."); 
     double avgInt = sumI/numInputInt; 
     double avgDoub = sumD/numInputDoub; 

     if (numInputInt > 0) { 
     System.out.println("\tAveragae of Integers: " + avgInt); 
     } 
     else { 
     System.out.println("\tNo intergers submitted"); 
     } 
     if (numInputDoub > 0) { 
     System.out.println("\tAverage of Doubles: " + avgDoub); 
     } 
     else { 
     System.out.println("\tNo doubles submitted"); 
     } 
     System.out.println(words); 


    } 

} 
+0

爲什麼使用'buffer.append(「」+ word);'? – MadProgrammer 2014-10-30 01:58:35

+0

事實上,爲什麼要使用'StringBuffer'呢? – MadProgrammer 2014-10-30 01:58:53

+0

我不能使用這個程序的其他類方法 – GPC 2014-10-30 02:00:01