国家给出了 8 岁男宝宝的标准身高为 130 厘米、标准体重为 27 公斤;8 岁女宝宝的标准身高为 129 厘米、标准体重为 25 公斤。
现在你要根据小宝宝的身高体重,给出补充营养的建议。
输入格式:
输入在第一行给出一个不超过 10 的正整数 N,随后 N 行,每行给出一位宝宝的身体数据:
性别 身高 体重
其中性别是 1 表示男生,0 表示女生。身高和体重都是不超过 200 的正整数。
输出格式:
对于每一位宝宝,在一行中给出你的建议:
如果太矮了,输出:duo chi yu!
(多吃鱼);
如果太瘦了,输出:duo chi rou!
(多吃肉);
如果正标准,输出:wan mei!
(完美);
如果太高了,输出:ni li hai!
(你厉害);
如果太胖了,输出:shao chi rou!
(少吃肉)。
先评价身高,再评价体重。两句话之间要有 1 个空格。
输入样例:
4
0 130 23
1 129 27
1 130 30
0 128 27
输出样例:
ni li hai! duo chi rou!
duo chi yu! wan mei!
wan mei! shao chi rou!
duo chi yu! shao chi rou!
//cpp
#include<iostream>
using namespace std;
class Baby{
public:
int sex ;
int height;
int weight;
};
int main(){
Baby B[10];
int N = 0;
cin>>N;
void heightpanduan (int flag, int height);
void weightpanduan (int flag, int weight);
for (int i = 0; i < N; i++){
cin>>B[i].sex;
cin>>B[i].height;
cin>>B[i].weight;
}
for (int i = 0; i < N; i++){
if (B[i].sex == 1){
heightpanduan (130, B[i].height);
weightpanduan (27, B[i].weight);
} else {
heightpanduan (129, B[i].height);
weightpanduan (25, B[i].weight);
}
}
return 0;
}
void heightpanduan (int flag, int height) {
if (height > flag) {
cout<<"ni li hai! ";
} else if (height == flag) {
cout<<"wan mei! ";
} else {
cout<<"duo chi yu! ";
}
}
void weightpanduan (int flag, int weight) {
if (weight > flag) {
cout<<"shao chi rou!"<<endl;
} else if (weight == flag) {
cout<<"wan mei!"<<endl;
} else {
cout<<"duo chi rou!"<<endl;
}
}
照例按着Cpp写Java
import java.util.Scanner;
class Baby{
int sex ;
int height;
int weight;
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
void heightpanduan (int flag, int height);
void weightpanduan (int flag, int weight);
Baby[] B = new Baby[10];
for (int i = 0; i < N; i++){
B[i].sex = sc.nextInt();
B[i].height = sc.nextInt();
B[i].weight = sc.nextInt();
}
for (int i = 0; i < N; i++){
if (B[i].sex == 1){
heightpanduan (130, B[i].height);
weightpanduan (27, B[i].weight);
} else {
heightpanduan (129, B[i].height);
weightpanduan (25, B[i].weight);
}
}
}
void heightpanduan (int flag, int height) {
if (height > flag) {
System.out.print("ni li hai! ");
} else if (height == flag) {
System.out.print("wan mei! ");
} else {
System.out.print("duo chi yu! ");
}
}
void weightpanduan (int flag, int weight) {
if (weight > flag) {
System.out.println("shao chi rou!");
} else if (weight == flag) {
System.out.println("wan mei!");
} else {
System.out.println("duo chi rou!");
}
}
}
报错
Main.java:14: error: illegal start of expression
void heightpanduan (int flag, int height);
^
Main.java:14: error: ‘;’ expected
void heightpanduan (int flag, int height);
^
Main.java:14: error: expected
void heightpanduan (int flag, int height);
^
Main.java:14: error: not a statement
void heightpanduan (int flag, int height);
^
Main.java:14: error: ‘;’ expected
void heightpanduan (int flag, int height);
^
Main.java:15: error: illegal start of expression
void weightpanduan (int flag, int weight);
^
Main.java:15: error: ‘;’ expected
void weightpanduan (int flag, int weight);
^
Main.java:15: error: expected
void weightpanduan (int flag, int weight);
^
Main.java:15: error: not a statement
void weightpanduan (int flag, int weight);
^
Main.java:15: error: ‘;’ expected
void weightpanduan (int flag, int weight);
^
10 errors
先去查了Java中方法(函数)的定义和使用
https://blog.csdn.net/hohiuching/article/details/77480471
将weightpanduan
和 heightpanduan
两个函数前面加了public
又报错
Main.java:25: error: non-static method heightpanduan(int,int) cannot be referenced from a static context
heightpanduan (130, B[i].height);
^ Main.java:26: error: non-static method weightpanduan(int,int) cannot be referenced from a static context
weightpanduan (27, B[i].weight);
^ Main.java:28: error: non-static method heightpanduan(int,int) cannot be referenced from a static context
heightpanduan (129, B[i].height);
^ Main.java:29: error: non-static method weightpanduan(int,int) cannot be referenced from a static context
weightpanduan (25, B[i].weight);
^ 4 errors
于是在public void 函数名,public和void中间加了个static
提交,跑通了但是啥也没输出
小伙伴: Baby[] B = new Baby[10];,这个之后还要在for循环里来一下。于是加了个B[i] = new Baby();
成功输出
注意啊:
Baby[] B = new Baby[10];**//这个在外面
for (int i = 0; i < N; i++){
B[i] = new Baby();//这个在里面
//java
import java.util.Scanner;
class Baby{
int sex ;
int height;
int weight;
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
//void heightpanduan (int flag, int height);
//void weightpanduan (int flag, int weight);
Baby[] B = new Baby[10];
for (int i = 0; i < N; i++){
B[i] = new Baby();
B[i].sex = sc.nextInt();
B[i].height = sc.nextInt();
B[i].weight = sc.nextInt();
}
for (int i = 0; i < N; i++){
if (B[i].sex == 1){
heightpanduan (130, B[i].height);
weightpanduan (27, B[i].weight);
} else {
heightpanduan (129, B[i].height);
weightpanduan (25, B[i].weight);
}
}
}
public static void heightpanduan (int flag, int height) {
if (height > flag) {
System.out.print("ni li hai! ");
} else if (height == flag) {
System.out.print("wan mei! ");
} else {
System.out.print("duo chi yu! ");
}
}
public static void weightpanduan (int flag, int weight) {
if (weight > flag) {
System.out.println("shao chi rou!");
} else if (weight == flag) {
System.out.println("wan mei!");
} else {
System.out.println("duo chi rou!");
}
}
}
以及还参考了这个博文
https://blog.csdn.net/wen_binobject/article/details/84957301