java入门小项目

java入门小项目

100内素数

public class sushu {
    public static void main(String[] args){
        int number = 0;
        for (int i = 2; i <= 100; i++){
          if (issushu(i)){
              System.out.print(i+" ");
              number ++;
              if (number % 5 == 0 )System.out.println();
          }
         }
        System.out.println("素数的个数为:" + number +" 个。" );
    }

    public static boolean issushu(int n){
        for (int j = 2; j < n ; j++){
            if (n % j == 0){
                return false;
            }
        }return true;
    }
}

超市

import java.util.Scanner;

public class Supermaket {
    public static void main(String[] args) {
        boolean  con = true;
        int choose,number;
        double sum = 0;
        Scanner scan = new Scanner(System.in);
        String str[] = {"牙刷","毛巾","水杯","苹果","香蕉"};
        double price[]={8.8,10.0,18.8,12.5,15.5};
        System.out.println("----------小商店-----------");
        for (int i = 0 ; i < 5 ; i++ ){
            System.out.println( (i+1) +". "+str[i]+"的价格为:"+price[i]);
        }
        do {
            System.out.print("请输入你需要购买的商品序列号:");
            choose = scan.nextInt();
            switch (choose){
                case 1:
                case 2:
                case 3:
                case 4:
                case 5: System.out.print("请输入你需要购买" + str[choose-1] + "的数量:");
                        break;
                default:System.out.println("输入有误");continue;
            }
            number = scan.nextInt();
            System.out.println("您购买了"+ str[choose-1] + number +"件, 需要花费 " + price[choose-1] * number + "元");
            sum += price[choose-1] * number;
            System.out.println("需要继续购买输入Y,否则输入N");
            String c = scan.next();
            if( c.equals("N")){
                System.out.println("您本次购物共消费 " + sum + "元");
                con = false;
            }
        }while(con);
    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值