团体程序设计天梯赛-练习集——L1-063 吃鱼还是吃肉(类数组、方法定义和使用)

国家给出了 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

weightpanduanheightpanduan两个函数前面加了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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,这段代码是用来统计输入数字中奇数和偶数的个数的。代码中使用循环遍历输入的数字,通过对2取模运算来判断数字是奇数还是偶数,然后分别对奇数和偶数的计数变量进行加一操作。循环结束后,输出奇数计数变量和偶数计数变量的值,中间用空格隔开。\[1\]\[2\]\[3\] 这段代码可以用来解决团体程序设计天梯-练习集 L1-022 奇偶分家的问题。 #### 引用[.reference_title] - *1* [PTA团队天梯║L1-022 奇偶分家](https://blog.csdn.net/m0_46492118/article/details/114481127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [PAT团队程序设计天梯-习题集L1-022 奇偶分家](https://blog.csdn.net/qq_38234015/article/details/81291913)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [团体程序设计天梯-练习集 L1-022 奇偶分家 (10分)(C语言)](https://blog.csdn.net/Baridhu/article/details/109899606)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值