查看: 1046|回复: 6
|
Java input String问题
[复制链接]
|
|
我正在试做一个java小programming,就是要用把user input的string一个字一个字放进array里面
首先
Scanner sc = new Scanner(System.in);
String [] word = new String[100];
System.out.print("Enter string :");
word[0]= sc.nextLine();
output出来就是整个名字,我要的是比如进Adam这个名字,program会自动把Adam放进word{'a','d','a','m'}。。差不多这样的,可是做不到,google好久也是不能...有那位高手会吗?教教我...谢谢
|
|
|
|
|
|
|
|
发表于 3-10-2013 10:00 PM
|
显示全部楼层
用 .toCharArray();- String words = "Java Expert";
- char[] charArray = words.toCharArray();
- System.out.println(charArray[0] + charArray[3] + charArray[7]);
- 答案:Jap
复制代码 |
|
|
|
|
|
|
|

楼主 |
发表于 3-10-2013 10:05 PM
|
显示全部楼层
Jason929 发表于 3-10-2013 10:00 PM 
用 .toCharArray();
哦。。我以为没人会回复我了..谢谢你啊。。。对java很有研究吗?可以问你其他问题吗?
|
|
|
|
|
|
|
|
发表于 3-10-2013 10:11 PM
|
显示全部楼层
shern91 发表于 3-10-2013 10:05 PM 
哦。。我以为没人会回复我了..谢谢你啊。。。对java很有研究吗?可以问你其他问题吗?
你外面的帖我有看到,看不明,你還是問回你的教授吧... |
|
|
|
|
|
|
|

楼主 |
发表于 3-10-2013 10:14 PM
|
显示全部楼层
Jason929 发表于 3-10-2013 10:11 PM 
你外面的帖我有看到,看不明,你還是問回你的教授吧...
聪明哦你。现在我头痛啊,我打算在第2题做两个method
import java.util.Scanner;
public class Aircond {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int choice;
System.out.print("1. Switch On Air Cond\n2. Switch Off Air Cond\n3. Exit\n");
while(true)
{
System.out.print("Enter your choice : ");
choice=sc.nextInt();
if(choice == 1)
{
switchON(null,null);
}
else if(choice == 2)
{
switchOFF(null);
}
else if(choice ==3)
{
System.exit(1);
}
else
{
System.out.println("This choice is not valid");
}
}
}
public static void switchON(String on,String on1) {
boolean a;
if (a=true)
{
on ="The air cond is ON";
System.out.println(on);
//return true;
}
if(a=false)
{
on1 ="The air cond is already ON";
System.out.println(on1);
a=true;
//return false;
}
}
private static void switchOFF(String off) {
off ="The air cond is OFF";
System.out.println(off);
}
}
/*
public static void switchON(String on,String on1) {
boolean a;
if (a=true)
{
on ="The air cond is ON";
System.out.println(on);
//return true;
}
if(a=false)
{
on1 ="The air cond is already ON";
System.out.println(on1);
a=true;
//return false;
}
}
private static void switchOFF(String off) {
off ="The air cond is OFF";
System.out.println(off);
}
可是我要在user选1后aircond on之后,user再选1的话就会出already on...我应该用boolean吗?
|
|
|
|
|
|
|
|
发表于 3-10-2013 10:29 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 3-10-2013 10:34 PM
|
显示全部楼层
Jason929 发表于 3-10-2013 10:29 PM 
你還是看有沒有其他大大得空回复你吧...
我懶惰想(其實題目也沒有看很明)...
哦,不用紧...有回复我就很好了...谢谢你进来给我答案...
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|