java循环中,我想在for循环中输入两次,第一次循环ok,但是第二次就不行了,求大神指点
答案:2 悬赏:0 手机版
解决时间 2021-01-26 07:16
- 提问者网友:留有余香
- 2021-01-25 20:01
java循环中,我想在for循环中输入两次,第一次循环ok,但是第二次就不行了,求大神指点
最佳答案
- 五星知识达人网友:白昼之月
- 2021-01-25 21:19
package com.maven.Test;
import java.util.Scanner;
public class TestMyBatis {
public static void main(String[] args) {
Dog dogs[]=new Dog[4];
Scanner sc=new Scanner(System.in);
double weight=0;
String name;
for(int i=0;i System.out.println("请输入第"+(i+1)+"只狗的名字:");
dogs[i]=new Dog();
name=sc.nextLine();
dogs[i].setName(name);
System.out.println(dogs[i].getName()); System.out.println("请输入第"+(i+1)+"只狗的体重:");
weight=Double.parseDouble(sc.nextLine());
dogs[i].setWeight(weight);
System.out.println(dogs[i].getWeight());
}
}
}
class Dog{
String name;
Double weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
}追问大神能解释以下为啥你这就可以吗,我将for中接收字符串的sc.nextLine函数换成sc.next也能运行了,但是不知道为啥
import java.util.Scanner;
public class TestMyBatis {
public static void main(String[] args) {
Dog dogs[]=new Dog[4];
Scanner sc=new Scanner(System.in);
double weight=0;
String name;
for(int i=0;i
dogs[i]=new Dog();
name=sc.nextLine();
dogs[i].setName(name);
System.out.println(dogs[i].getName()); System.out.println("请输入第"+(i+1)+"只狗的体重:");
weight=Double.parseDouble(sc.nextLine());
dogs[i].setWeight(weight);
System.out.println(dogs[i].getWeight());
}
}
}
class Dog{
String name;
Double weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
}追问大神能解释以下为啥你这就可以吗,我将for中接收字符串的sc.nextLine函数换成sc.next也能运行了,但是不知道为啥
全部回答
- 1楼网友:鸠书
- 2021-01-25 22:29
你查查next()和nextLine()的区别。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯