switch; while; if exercise

Posted by titel on Thu, 28 Nov 2019 17:07:46 +0100

Please enter the consumption amount:
500
Whether to participate in preferential exchange activities
1: 50 yuan or more, 2 yuan for Pepsi
2: 100 yuan or more, and 3 yuan for 500 ml cola
3: for 100 yuan, add 10 yuan to buy 5kg flour
4: for 200 yuan or more, add 10 yuan to buy 1 SUPOR frying pan
5: for 200 yuan, add 20 yuan to buy a bottle of L'Oreal toner
import java.util.Scanner;

public class Helloworld {

	public static void main(String[] args) {
		System.out.println("Please enter the consumption amount:");
		Scanner input=new Scanner(System.in);
		int money=input.nextInt();
		System.out.println("Whether to participate in preferential exchange activities");
		System.out.println("1:Buy 1 Pepsi drink for 50 yuan or more");
		System.out.println("2:RMB 100 or more, RMB 3 for 500 ml Coke 1 bottles");
		System.out.println("3:For 100 yuan, add 10 yuan to buy 5 kg flour");
		System.out.println("4:For 200 yuan, add 10 yuan to buy 1 SUPOR wok");
		System.out.println("5:For more than 200 yuan, add 20 yuan to buy one bottle of L'Oreal toner");
		System.out.println("Please choose:");
		int num=input.nextInt();
		switch (num) {
		case 1:
			System.out.println("Total amount of this consumption:"+(money+2));
			System.out.println("Successful replacement: Pepsi cola drink 1 bottle");
			break;
		case 2:
			System.out.println("Total amount of this consumption:"+(money+3));
			System.out.println("Successful replacement: Pepsi cola drink 1 bottle");
			break;
		case 3:
			System.out.println("Total amount of this consumption:"+(money+10));
			System.out.println("Successful replacement: Pepsi cola drink 1 bottle");
			break;
		case 4:
			System.out.println("Total amount of this consumption:"+(money+10));
			System.out.println("Successful replacement: Pepsi cola drink 1 bottle");
			break;
		case 5:
			System.out.println("Total amount of this consumption:"+(money+20));
			System.out.println("Successful replacement: Pepsi cola drink 1 bottle");
			break;

		default:
			break;
		}

	}

2

Please select the item number to purchase:
1.T-shirt 2. Tennis shoes 3. Tennis racket
Please enter the item number:
1
Please enter the quantity purchased
2
T-shirt ¥ 245.0 ¥ quantity 2 ¥ 490.0 in total
Continue (y/n)
import java.util.Scanner;

public class buy {

public static void main(String[] args) {
	String  answer;
    double sum1 = 0;
	double sum2 = 0;
	double sum3 = 0;
	Scanner input=new Scanner(System.in);
do {
	System.out.println("******************");
	System.out.println("Please select the item number to purchase:");
	System.out.println("1.T Shirt\t2.Tennis shoes\t3.Tennis racket\t");
	System.out.println("Please enter the item number:\t");
	int s1=input.nextInt();
	if (s1==1) {
		System.out.println("Please enter the quantity purchased");
		int num=input.nextInt();
		 sum1=245.0*num;
		System.out.println("T Shirt¥245.0\t"+"\t"+"Number"+num+"\t"+"Total¥"+sum1+"\t");
	    }else if (s1==2) {
		System.out.println("Please enter the quantity purchased");
		int num=input.nextInt();
		 sum2=570.0*num;
		System.out.println("Tennis shoes¥570.0\t"+"\t"+"Number"+num+"\t"+"Total¥"+sum2+"\t");
		}else{
			System.out.println("Please enter the quantity purchased");
			int num=input.nextInt();
			 sum3=320.0*num;
			System.out.println("Tennis racket¥320.0\t"+"\t"+"Number"+num+"\t"+"Total¥"+sum3+"\t");
	}
    System.out.println("Continue or not( y/n)");
     answer=input.next();
} while (!"n".equals(answer));  
System.out.println("Discount: 0.8");
System.out.println("Amount payable:"+(sum1+sum2+sum3)*0.8);
System.out.println("Paid in amount:");
int money=input.nextInt();
System.out.println("Give change   ¥"+(money-(sum1+sum2+sum3)*0.8)+"\t");
}

3. Use do while to realize: output the comparison table of Celsius temperature and Fahrenheit temperature. It is required that the temperature of Celsius starts from 0, and every 20 degrees is one item, with no more than 10 items
public class wendu {

public static void main(String[] args) {
	double T=0;
	int n=0;
	double f=0;
	do{
		f=((T*9)/5.0)+32;
		System.out.println("Degree Celsius:"+T+"Huacentigrade is:"+f);	
		T=T+20;n++;
	}while(n<=10&&T<=250);
}

Topics: Java