佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1046|回复: 6

Java input String问题

[复制链接]
发表于 29-9-2013 04:00 PM | 显示全部楼层 |阅读模式
我正在试做一个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好久也是不能...有那位高手会吗?教教我...谢谢

回复

使用道具 举报


ADVERTISEMENT

发表于 3-10-2013 10:00 PM | 显示全部楼层

.toCharArray();
  1. String words = "Java Expert";
  2. char[] charArray = words.toCharArray();
  3. System.out.println(charArray[0] + charArray[3] + charArray[7]);
  4. 答案: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 | 显示全部楼层
shern91 发表于 3-10-2013 10:14 PM
聪明哦你。现在我头痛啊,我打算在第2题做两个method

import java.util.Scanner;


你還是看有沒有其他大大得空回复你吧...

我懶惰想(其實題目也沒有看很明)...
回复

使用道具 举报

Follow Us
 楼主| 发表于 3-10-2013 10:34 PM | 显示全部楼层
Jason929 发表于 3-10-2013 10:29 PM
你還是看有沒有其他大大得空回复你吧...

我懶惰想(其實題目也沒有看很明)...

哦,不用紧...有回复我就很好了...谢谢你进来给我答案...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 14-9-2025 04:20 AM , Processed in 0.142004 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表