java第四次作业

  1. 某公司年会抽奖

switch语句:

package chouj;

import java.util.Scanner;

public class Choujj {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
System.out.print("请输入奖号:");
        Scanner jp=new Scanner(System.in);           
        int A=jp.nextInt();                           
        switch(A){                                    
        case 1:
            System.out.print("一等奖:42寸彩电");break;
        case 2:
            System.out.print("二等奖:微波炉");break; 
        case 3:
            System.out.print("三等奖:加湿器");break;  
        case 4:
            System.out.print("安慰奖:U盘-16G");break;
        default :
            System.out.print("没有中奖"); 
           jp.close();  
        }
    }
}

if else语句

package chouj;

import java.util.Scanner;

public class Chouj {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
System.out.print("请输入您的奖号:");
        Scanner jp=new Scanner(System.in);
          int a= jp.nextInt();
          if(a==1){
          System.out.println("恭喜获得一等奖:42寸彩电");
          }else if(a==2)
          {
          System.out.println("恭喜获得二等奖:光波炉");
          }else if(a==3)
          {
          System.out.println("恭喜获得三等奖:加湿器");
          }else if(a==4)
          {
          System.out.println("恭喜获得四等奖:16G-U盘");
          }else{
          System.out.println("输入错误!");
          }
       jp.close();
    }
       }
  1. 百度一下

switch语句写法:
import java.util.Scanner;

public class danbaiz {

    public static void main(String[] args) {
        System.out.print("请输入公司名称:");
        Scanner gs=new Scanner(System.in);
        String s = gs.nextLine();                    
        switch(s) {         
        case"明日科技":System.out.print("明日科技主要出版专业的互联网编程语言书籍");
        break;
        case"京东集团":System.out.print("主要经营网上购物商城");break;
        case"阿里巴巴":System.out.print("国内最大的互联网购物平台之一");break;
        default:System.out.print("未收录或不存在");
        gs.close();
        }

    }

}
if else写法
public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("请您输入公司的名称:");
         Scanner name =new Scanner(System.in);;
        String a= name.nextLine();
        if(true==a.equals("明日科技")) {
            System.out.println("明日科技主要出版专业的互联网编程语言书籍");
        }else if(true==a.equals("京东集团")) {
            System.out.println("主要经营网上购物商城");
        }else if(true==a.equals("阿里巴巴")) {
                System.out.println("国内最大的互联网购物平台之一");
            }else{
                System.out.println("未收录或不存在");
            }
            name.close();
        }
    }
  1. 查询高考录取分数线

if else 写法
Scanner xue=new Scanner(System.in);
System.out.println("请输入要查询的录取分数线(比如民办本科,艺术类本科,体育类本科,二本,一本):");
         String a= xue.next();
         if(true==a.equals("一本")) {
             System.out.println("一本录取分数线:555分");
         }else if(true==a.equals("二本") ){
             System.out.println("二本录取分数线:445分");
         }else if(true==a.equals("艺术类本科") ){
             System.out.println("艺术类本科录取分数线:290分");
         }else if(true==a.equals("体育类本科") ){
             System.out.println("体育类本科录取分数线:280分");
         }else if(true==a.equals("民办本科") ){
             System.out.println("艺术类本科录取分数线:350分");
         }else {
             System.out.println("未查询到此分数线");
         }
         xue.close();
          }
        
    }
switch
public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner xue=new Scanner(System.in);
        System.out.println("请输入要查询的录取分数线(比如民办本科,艺术类本科,体育类本科,二本,一本):");
        String a= xue.nextLine();
         switch(a){
             case "二本": System.out.println("二本录取分数线:445分");
             break;
             case "一本":System.out.println("一本录取分数线555分");
             break;
             case "体育类本科":System.out.println("体育类本科分数线:280分");
             break;
             case "艺术类本科":System.out.println("艺术类本科分数线:290分");
             break;
             case "民办本科":System.out.println("民办本科分数线:350分");
             default: 
             System.out.println("未查询到此分数线");
             break;
           }
         xue.close();
      }
        
    }
4.计算 2+4+6+8+……+100的值
public static void main(String[] args) {
        // TODO Auto-generated method stub
        int sum = 0;
        for (int i = 2; i <= 100; i += 2) {
            sum += i;
        }
        System.out.println(sum);
        
    }

}
5.输入十个数,并求这十个数的乘积
public static void main(String[] args) {
        // TODO Auto-generated method stub
        int b=1;
        System.out.println("请输入十个数:");
        for(int i=1;i<=10;i++){/
        Scanner sc=new Scanner(System.in);//创建了一个新的对象叫sc
        int a=sc.nextInt();//将数字sc赋予给a          
        b*=a;
        }
        System.out.println(b);//输出b的值 
    }
    
}

6.输出0到9的数,但不包括6

for循环iF条件语句continur跳过

package fooo;

public class For {

    public static void main(String[] args) {
        // TODO Auto-generated method stub            
                   for(int i=0;i<10;i++){
                        if(i==6){
                        continue;    
                        }
                        System.out.print(i+"");
                            
                        }
                    }
                    
            }
    
7.输入一个正整数 n,然后打印出1到n 中的所有素数
package shdfjd;

public class Fnmvk {
    
    public static void main(String[] args) {
        int n=40;
        System.out.print(n+"素数有:");
       
        for(int i=2; i<n; ++i){
           
        boolean flag=true;
            
        for(int j=2; j<i; ++j){
          
        if(i%j == 0){
          flag=false; 
             break;
                }
            }
            if(flag){
                System.out.print(i+" ");
            }
        }
        System.out.println();
    }
}
8.九九乘法表
for(int i = 1; i <= 9; i++) {
 
            for(int j = 1; j <= i; j++) {
                
                System.out.print(j + "*" + i + "=" + (j * i) + " ");
            }
           
            System.out.println();
        }
    }
}
9.输入一个年份n,然后输出从 1949 年到 n 年中所有的闰年
public static void main(String[] args) {
        // TODO Auto-generated method stub            
        Scanner input = new Scanner(System.in);
        System.out.print("请输入一个年份n:");
        int n = input.nextInt();
        System.out.print("1949年到" + n + "年中的闰年有:");
        for (int i = 1949; i <= n; i++) {
            if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
                System.out.print(i + " ");
            input.close();
           }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云玩java.dog️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值