java中用if语句做输入四位数卡号的和获得奖品

一介小白的代码......

public static void main(String[] args) {
        
//会员卡号和兑换奖品
        
        System.out.println("请输入你的会员卡号:");
        Scanner sc = new Scanner(System.in);
        int card = sc.nextInt();
        System.out.println("你的会员卡号为:"+card);

//  分别单独求出卡号的每一位数字 然后相加
        int qianwei = card/1000;
        int baiwei = card/100%10;
        int shiwei = card/10%10;
        int gewei = card%10;


        int he = qianwei+baiwei+shiwei+gewei;
        
        System.out.println("千位:"+qianwei+"百位:"+baiwei+"十位:"+shiwei+"各位:"+gewei);
        
        System.out.println("会员卡号和为:"+he);
        

// 简单的if语句判定有没要达到要求
        if (he>=30) {
            System.out.println("大兄弟!牛蛙牛蛙!一等奖!");
        }else if(he>=20) {
            System.out.println("可以啊!小子!二等奖!");
        }else {
            System.out.println("没得奖!走开走开!");
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用if-else if-else语句和do-while循环来编写猜拳游戏。 首先,让我们来介绍一下if-else if-else语句。这种语句结构允许根据条件的不同执行不同的代码块。在猜拳游戏中,我们可以使用if-else if-else语句来判断玩家和电脑的出拳结果,并根据不同的情况给出相应的提示。 下面是一个简单的示例代码: ```java import java.util.Scanner; public class RockPaperScissors { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入你的选择(1-石头,2-剪刀,3-布):"); int playerChoice = scanner.nextInt(); // 生成电脑的选择(1-石头,2-剪刀,3-布) int computerChoice = (int) (Math.random() * 3) + 1; if (playerChoice == computerChoice) { System.out.println("平局!"); } else if ((playerChoice == 1 && computerChoice == 2) || (playerChoice == 2 && computerChoice == 3) || (playerChoice == 3 && computerChoice == 1)) { System.out.println("你赢了!"); } else { System.out.println("你输了!"); } } } ``` 接下来,让我们来介绍一下do-while循环。这种循环结构会先执行一次循环体中的代码,然后再根据条件判断是否继续执行循环。在猜拳游戏中,我们可以使用do-while循环来实现多次猜拳的功能,直到玩家选择退出游戏。 下面是一个简单的示例代码: ```java import java.util.Scanner; public class RockPaperScissors { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String playAgain; do { System.out.println("请输入你的选择(1-石头,2-剪刀,3-布):"); int playerChoice = scanner.nextInt(); // 生成电脑的选择(1-石头,2-剪刀,3-布) int computerChoice = (int) (Math.random() * 3) + 1; if (playerChoice == computerChoice) { System.out.println("平局!"); } else if ((playerChoice == 1 && computerChoice == 2) || (playerChoice == 2 && computerChoice == 3) || (playerChoice == 3 && computerChoice == 1)) { System.out.println("你赢了!"); } else { System.out.println("你输了!"); } System.out.println("是否继续游戏?(输入Y继续,其他键退出)"); playAgain = scanner.next(); } while (playAgain.equalsIgnoreCase("Y")); } } ``` 以上就是使用if-else if-else语句和do-while循环编写猜拳游戏的示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值