1、首先打开eclipse,创建一个Java工程项目,并在src下创建类Main,创建后的工程目录如图。
2、接着在Main.java中加入main函数和要输出的数组的内容。
3、然后将数组中的数据按每行10个输出到控制台上。
4、按上面代码编辑完Main.java后,右键点击用java应用程序的方式运行Main.java,结果如图。
5、从图中可以看到,数组中30个数字按每行10个的形式输出到了控制台。
public static void main(String[] args) throws IOException {
System.out.println("~欢迎光临索迪动物园~");
while(true){
System.out.print("请输入动物的名字,Dog Cat Frog ;输入exit退出!") ;
InputStreamReader reader = new InputStreamReader(System.in);
String input = new BufferedReader(reader).readLine();
if("exit".equals(input) ){
System.out.println("再见!");
break ;
}else if( !"Dog".equals(input) && !"Cat".equals(input) &&!"Frog".equals(input) ){
System.out.println("输入错误,请重新输入.");
continue ;
}
if("Dog".equals(input) ){
System.out.println("狗");
}else if("Cat".equals(input) ){
System.out.println("猫");
}else if("Frog".equals(input) ){
System.out.println("青蛙");
}
}
}
import java.util.Scanner;
public class ZooDemo {
public static void main(String[] args) {
welcome();
}
public static void welcome() {
System.out.println("~欢迎光临索迪动物园~");
System.out.println("请输入动物的名称:Dog、Cat和Frog;输出exit退出!");
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String line = scan.next();
if (line.equalsIgnoreCase("exit")) {
System.out.println("再见!");
break;
} else if (line.equalsIgnoreCase("DOG")) {
System.out.println("摇摇尾巴");
} else if (line.equalsIgnoreCase("CAT")) {
System.out.println("喵喵~");
} else if (line.equalsIgnoreCase("FrOG")) {
System.out.println("呱呱~");
} else {
System.out.println("请重新输入");
}
System.out.println("请输入动物的名称:Dog、Cat和Frog;输出exit退出!");
}
}
}
放在一个while循环里如
while(true){
//代码
}