用while和for循环输出1-1000之间能被5整除的数,且每行输出三个。
public class Test1 {
public static void main(String[] args) {
//用while和for循环输出1-1000之间能被5整除的数,且每行输出三个。
int j = 0;
for (int i = 1; i <= 1000 ; i++) {
if(i % 5 ==0){
System.out.print(i + "\t");
j++;
if(j%3 ==0){
System.out.println();
j = 0;
}
}
}
}
}
购物结算
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
//购物结算
System.out.println("***************************************************************************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤 \t 2.网球鞋\t 3.网球拍");
System.out.println("***************************************************************************************************");
//创建Scanner对象
Scanner sc = new Scanner(System.in);
String flag ="y";
//存储商品编号
int pld = 0;
//存储商品编号的变量
int num = 0;
//T恤单价
double tPrice = 245.0;
//网球鞋单价
double sPrice = 570.0;
//网球拍单价
double pPrice = 320.0;
//定义每次购买商品总价
double oneTotalPrice = 0;
//定义一个计算购买商品总额的变量
double totalPrice = 0;
while("y".equals(flag)){
System.out.println();
System.out.println("请输入商品编号:");
pld =sc.nextInt();
//用if 判断是否正确的输入商品编号
if(pld != 1 && pld != 2 && pld != 3){
System.out.println("亲,你买的商品不存在");
//跳出当前循环进行下一次循环
continue;
}
System.out.println("请输入购买数:");
num = sc.nextInt();
switch(pld){
case 1:
oneTotalPrice = tPrice*num;
System.out.println("T恤¥" + tPrice + "\t" + "数量" + num + "\t" + "合计" + "¥" + oneTotalPrice);
totalPrice += oneTotalPrice;
//清0
oneTotalPrice = 0;
break;
case 2:
oneTotalPrice = sPrice * num;
System.out.println("网球鞋¥" + sPrice + "\t" + "数量" + num + "\t" + "合计" + "¥" + oneTotalPrice);
totalPrice += oneTotalPrice;
//清0
oneTotalPrice = 0;
break;
case 3:
oneTotalPrice = pPrice * num;
System.out.println("网球拍¥" + pPrice + "\t" + "数量" + num + "\t" + "合计" + "¥" + oneTotalPrice);
totalPrice += oneTotalPrice;
//清0
oneTotalPrice = 0;
break;
}
System.out.println("是否继续(y/n):");
flag = sc.next();
}
System.out.println();
System.out.println("折扣:");