java.输入水果的编号,求它对应的单价

总结:可以if-else 循环或switch循环

package com.b;

import java.util.Scanner;

/*以下4种水果的单价分别是3.00元/公斤,2.50元/公斤,4.10元/公斤,10.20元/公斤。
//输入水果的编号,输出它的单价,若输出不正确的编号,则显示单价为0;
[1] apples

[2] pears

[3] oranges

[4] grapes<!--> //->*/
public class dfhgrr {
	public static void main(String[] args) {
		Scanner c=new Scanner(System.in);
		int y=c.nextInt();
	System.out.println("输入水果的编号");
//int x=c.nextInt();
	int choice=0;
		double price = 0;
		
			
		
	for(int i=0;i<y;i++){
		
		choice=c.nextInt();
		if(choice==1){
			price=3.0;
		}else if(choice==2){
			price=2.5;
		}else if(choice==3){
			price=4.1;
		}else if(choice==4){
			price=10.2;
		}
		System.out.println("单价是:"+price+"元/公斤");
		
	}
	
	
	}
	
}
//
3
输入水果的编号
2
单价是:2.5元/公斤

1
单价是:3.0元/公斤
4
单价是:10.2元/公斤

  

原文地址:https://www.cnblogs.com/langlove/p/3407838.html