2013-11-26 62 views
0

因此,我正在为CSCI课程做这项任务,我无法将自己的大脑包围在浩然之中,做它需要我做的事情。继承人的任务是什么的一部分,说:“提示用户佩斯颜色/香料,然后将它们存储在一个阵列或阵列状结构免除以相反的顺序的颜色/口味。” 而我的继承人代码:以相反的顺序打印一组单词

import java.util.Scanner; 
import java.util.Arrays; 

public class PezDispenser { 

public static void main(String[] args){ 

Scanner input = new Scanner(System.in); 
     String col1; 
     String col2; 
     String col3; 
     String col4; 
     String col5; 
     String col6; 
     String col7; 
     String col8; 
     String col9; 
     String col10; 

System.out.println("I need ten colors or favors"); 

     col1 = input.nextInt(); 
     col2 = input.nextInt(); 
     col3 = input.nextInt(); 
     col4 = input.nextInt(); 
     col5 = input.nextInt(); 
     col6 = input.nextInt(); 
     col7 = input.nextInt(); 
     col8 = input.nextInt(); 
     col9 = input.nextInt(); 
     col10 = input.nextInt(); 

String[] myArray = new String[] {col1 + col2 + col3 + col4 + col5 + col6 + col7 + col8 + col9 + col10}; 


System.out.println("Now to dispense"); 

System.out.println(myArray.asList.reverse(myArray)); 
} 

}

问题是,这只适用于数字,键入一个字母在程序崩溃。

+0

你需要'nextLine()'或'next()'作为'Strings' –

回答

0

问题是,这只适用于数字,在 键入一个字母崩溃的程序。

当你输入串的程序崩溃是由于该行的:

col1 = input.nextInt(); 
col2 = input.nextInt(); 
col3 = input.nextInt(); 
col4 = input.nextInt(); 
col5 = input.nextInt(); 
col6 = input.nextInt(); 
col7 = input.nextInt(); 
col8 = input.nextInt(); 
col9 = input.nextInt(); 
col10 = input.nextInt(); 

input.nextInt()将只读取整数。

对于阅读strings可以使用next()nextLine()这样的:

col1 = input.nextLine(); 
col2 = input.nextLine(); 
col3 = input.nextLine(); 
col4 = input.nextLine(); 
col5 = input.nextLine(); 
col6 = input.nextLine(); 
col7 = input.nextLine(); 
col8 = input.nextLine(); 
col9 = input.nextLine(); 
col10 = input.nextLine(); 
+1

非常感谢,我没有注意到我有.nextInt,我尝试了几个变种来让它工作,我想我忘了我有在 – user3037596

+0

@ user3037596: - 不客气!不管有时甚至最好提交这些东西!快乐编码;) –

+0

是这一行ok:System.out.println(myArray.asList.reverse(myArray)); – user3037596

0

input.nextInt()意味着只有整数。因此,字符串的问题。

如果您需要字符串,您可以使用next()或nextLine()(对于nextLine(),输入必须在每次输入后用新行输入)。

+0

感谢很多人的快速反应,我没有注意到我有.nextInt,我会给那个当我回家的时候尝试一下 – user3037596

相关问题