首页 > 编程语言 >Java练习1

Java练习1

时间:2022-11-16 21:11:59浏览次数:39  
标签:Java int 练习 System electricity template println out


*/
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名:text.cpp
* 作者:常轩
* 微信公众号:Worldhello
* 完成日期:2016年9月20日
* 版本号:V1.0
* 程序输入:无
* 程序输出:见运行结果
*/


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Qing
*/
import java.util.Scanner;
public class Test {
public static void main(String args[]){

Scanner reader=new Scanner(System.in);
//P40 实验3-1 将三个整数从小到大进行排序
System.out.println("Please input three number.");
int Num1,Num2,Num3;
Num1=reader.nextInt();
Num2=reader.nextInt();
Num3=reader.nextInt();
Number n=new Number();
n.sort(Num1, Num2, Num3);
/*-------------------------------------------------------------------*/
//P42 实验3-2 根据彩票号的末尾号码来判断所创建对象的中奖情况
int number;
System.out.println("Please input your number:");
number=reader.nextInt();
Administrator person=new Administrator();
person.juage(number);
/*-------------------------------------------------------------------*/
//P46 实验1 根据用户电量计算电费
double electricity;
System.out.println("Please input electricity:");
electricity=reader.nextDouble();
Calculation family=new Calculation();
family.calcu(electricity);
/*-------------------------------------------------------------------*/
//P47 实验2 猜数字游戏,提示玩家猜大了还是猜小了
Guess student=new Guess();
student.guessnumber();
/*-------------------------------------------------------------------*/
//P49 练习4 用for循环输出俄文字母表
Alphabet one=new Alphabet();
one.pAlphabet();
/*--------------------------------------------------------------------*/
//P49 练习5 编写一个程序求1!+2!+.....+20!
Factorial two=new Factorial();
two.pfactorial();
/*-------------------------------------------------------------------*/
//P49 练习6 求1000以内的所有完数
PerfectNumber three=new PerfectNumber();
three.findPerfectnumber();
/*------------------------------------------------------------------*/
}
}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Qing
*/
public class Administrator {
void juage(int number){ //用switch语句来实现判断
if(number<=0||number>99999){
System.out.println("Input error!Please enter the five digit lottery number");
}else{
int d1=number%10;
int d2=number%100;
int d3=number%1000;
switch(d1){
case 1:
case 3:
case 9:
System.out.println("Lottery is the three prize!");
break;
default:
System.out.println("Lottery is not the three prize");
}
switch(d2){
case 29:
case 46:
case 21:
System.out.println("Lottery is the two prize");
break;
default :
System.out.println("Lottery is not the two prize");
}
switch(d3){
case 875:
case 326:
case 596:
System.out.println("Lottery is the one prize");
break;
default:
System.out.println("Lottery is not the one prize");
}
}

}

//用if-else if-else 多条件分支语句代替switch语句来实现判断
/*
void judge(int number){
if(number<=0||number>99999){
System.out.println("Input error!Please enter the five digit lottery number");
}else{
int d1=number%10;
int d2=number%100;
int d3=number%1000;
if(d1==1||d1==3||d1==9){
System.out.println("Lottery is the three prize!");
if(d1==1||d1==9){
if(d2/10==2){
System.out.println("Lottery is the two prize");
}
}
}else if(d2==21||d2==29||d2==46){
System.out.println("Lottery is the two prize");
}else if(d3==875||d3==326||d3==596){
System.out.println("Lottery is the one prize");
}
}
}
*/
}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Qing
*/
public class Alphabet {
void pAlphabet(){
char c1 = 'А';
char c2 = 'а';
for (int i = c1; i < c1 + 32; i++) {
System.out.print((char) i+""+(char)(c2++)+" ");
}
}
}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Qing
*/
public class Calculation {
void calcu(double electricity){
double elecFee=0;
if(electricity<0){
System.out.println("Input error!");
}
else{
if(electricity<=90){
elecFee=electricity*0.6;
}
if(electricity>=91&&electricity<=150){
elecFee=90*0.6+(electricity-90)*1.1;
}
if(electricity>150){
elecFee=90*0.6+60*1.1+(electricity-150)*1.7;
}
}
System.out.printf("ElectricityFees is:%5.2f",elecFee);
}
}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Qing
*/
public class Factorial {
void pfactorial(){
int sum=0,product=1;
int i,j;
for(i=1;i<=20;i++){
for(j=1;j<=i;j++){
product=product*j;
}
sum=sum+product;
product=1;
}
System.out.println("Result is:"+sum);
}
}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Qing
*/
import java.util.Scanner;
import java.util.Random;
public class Guess {
void guessnumber(){
Scanner reader=new Scanner(System.in);
Random random=new Random();
int gNumber;
int rNumber=random.nextInt(100)+1; //产生1-100之间的整数
System.out.println("Give you an integer between 1-100, please guess the number:");
System.out.println("Please enter your guess:");
gNumber=reader.nextInt();
if(gNumber<1||gNumber>100){
System.out.println("Input error!please again.");
gNumber=reader.nextInt();
}
while(gNumber!=rNumber){
if(gNumber>rNumber){
System.out.println("Too big,please input again!");
gNumber=reader.nextInt();
}
else{
System.out.println("Too small,please input again!");
gNumber=reader.nextInt();
}
}
System.out.println("You are right!");
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Qing
*/
public class PerfectNumber {
void findPerfectnumber(){
int factorSum=0;
int i,j;
for(i=2;i<=1000;i++){
for(j=1;j<=i/2;j++){
if(i%j==0){
factorSum=factorSum+j;
}
}
if(factorSum==i){
System.out.println(i);
}
factorSum=0;
}
}
}



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Qing
*/
public class Number {
void sort(int n1,int n2,int n3){
int temp=0;
if(n2<n1){
temp=n1;
n1=n2;
n2=temp;
System.out.println("The first sort result is :"+n1+"、"+n2+"、"+n3);
}
if(n3<n1){
temp=n3;
n3=n1;
n1=temp;
System.out.println("The second sort result is :"+n1+"、"+n2+"、"+n3);
}
if(n3<n2){
temp=n3;
n3=n2;
n2=temp;
System.out.println("The end sort result is :"+n1+"、"+n2+"、"+n3);
}
}
}


标签:Java,int,练习,System,electricity,template,println,out
From: https://blog.51cto.com/u_15311571/5857656

相关文章

  • Javascript(笔记39) - ES6特性 - 集合Set
    SETES6 提供了新的数据结构set(集合)。集合类似于数组,但成员的值都是唯一的,集合实现iterator 接口,所以可以使用“扩展运算符”和“for...of”进行遍历,集合的属性和方法......
  • 肖sir__Java中spring boot
    1.1springboot介绍什么是SpringBoot?SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来......
  • [JavaScript]自定义排序方式Array.sort
    自定义排序方式,通过array.sort//按助力值、绑定时间排序。return<0:a在前,return>0:a在后,return==0:不变list.sort(function(a,b){varref=0if(a.bi......
  • 封装,继承(super,this,方法重写),多态--JAVA
    一、封装封装:就是把抽象出的数据【属性】和对数据的操作【方法】封装在一起,数据被保护在内部,程序的其他部分只有通过被授权的操作才能对数据进行操作  publicclass......
  • 一道容易犯错的Java String面试题
    分析下面这段代码,说明总共创建了多少个对象?程序的输出结果是什么?publicclassDemo{publicstaticvoidmain(String[]args){Stringa="hello";......
  • Java中ThreadLocal详解
    一、ThreadLocal简介        ThreadLocal叫做线程变量,意思是ThreadLocal中填充的变量属于当前线程,该变量对其他线程而言是隔离的,也就是说该变量是当前线程独有的......
  • Java 内存模型(JMM)
    1.为什么要有内存模型?要想回答这个问题,我们需要先弄懂传统计算机硬件内存架构。好了,我要开始画图了。1.1.硬件内存架构(1)CPU去过机房的同学都知道,一般在大型服务器上......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:数组中的第K个最大元素
    题目:给定整数数组nums和整数k,请返回数组中第k个最大的元素。请注意,你需要找的是数组排序后的第k个最大的元素,而不是第k个不同的元素。你必须设计并实现时间复杂度......
  • 【Java】Synchronized与ReentrantLock区别总结
    这篇文章是关于这两个同步锁的简单总结比较,关于底层源码实现原理没有过多涉及,后面会有关于这两个同步锁的底层原理篇幅去介绍。相似点:这两种同步方式有很多相似之处,它们......
  • Java @Data注解
    1、@Data注解是lombok.jar包下的注解,该注解通常用在实体bean上,不需要写出set和get方法,但是具备实体bean所具备的方法,简化编程提高变成速度。 2、@Data相当于@Getter@Sett......