javaif作業
㈠ java作業!!
publicstaticvoidmain(String[]args){
容inta=10,num=100;
while(num<1000){
if(a*a==num){
System.out.println(a*a);
}
a++;
num=a*a;
}
}
㈡ JAVA用if條件結構做
這個應該滿足你的要求。試試看吧。
是不是你們發的一個作業啊?
看到這個題三次了。好好理解理解吧。
import java.util.Scanner;
public class Test {
private final static char USER_NAME = '青';
private final static int PASSWORD = 123;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("請輸入用戶名:");
String userName = input.next();
System.out.print("請輸入密碼:");
int passWord = input.nextInt();
if (userName.length() == 1 && userName.charAt(0) == USER_NAME && passWord == PASSWORD) {
System.out.println("歡迎你,青");
} else {
System.out.println("對不起,你不是青");
}
}
}
㈢ java作業!!!!
abstract class Check {
/**
* @param args
*/
public Check(){};
public void outHello(){
System.out.println("Hello");
}
public abstract void abstractM();//抽象方法
}
public class extendCheck extends Check{
public static void main(String[] args) {
//成功版 輸出權father Hello Hello
Check check = new extendCheck();
check.abstractM();
check.outHello();
//
// 報錯Check check2 = new Check();
}
@Override
public void abstractM() {
System.out.println("father Hello");
}
}
㈣ java 中if條件的用法請求詳細解說
public class compare{ //定義類
static int a,b;
public static int function(int a,int b){
int c;
if(a>=b)
c=a;
this.a=b;
this.b=c;
}
public static void main(String[] args){
compare.function(x,y);
System.out.println("a:"+compare.a+"b:"+compare.b);
}
㈤ JAVA作業怎麼做
importjava.util.Scanner;
publicclassGuessGame{
publicstaticvoidmain(String[]args){
startGuess();
}
publicstaticvoidstartGuess(){
Scannerin=newScanner(System.in);
intinput,random,fower=1,upper=13;
inti=1;
System.out.println("請開始輸入想猜的正整數:");
while(true){
input=in.nextInt();
//生成fower到upperde隨機數
random=(int)(Math.random()*(upper-fower+1)+fower);
if(input>random){
System.out.println("當前隨機數"+random+"范圍("+fower+","+upper+")");
System.out.println("大了");
upper=input-1;
fower=1;
}elseif(input<random){
System.out.println("當前隨機數"+random+"范圍("+fower+","+upper+")");
System.out.println("小了");
upper=13;
fower=input+1;
}elseif(i==1){
System.out.println("第一次輸入就猜中,猜"+(int)(Math.random()*(upper-fower+1)+fower)+"的人將被懲罰!");
}else{
System.out.println("當前隨機數"+random+"范圍("+fower+","+upper+")");
System.out.println("猜中的人被懲罰");
System.out.println("GAMEOVER!");
break;
}
i++;
}
}
}
㈥ java-if嵌套結構的一個作業
publicclassUtil{
publicstaticvoidmain(String[]args){
.util.Scannersc=newjava.util.Scanner(System.in);
int[]arr=newint[3];
for(inti=0;i<arr.length;i++){
System.out.println("請輸入第"+(i+1)+"個整數!");
try{
arr[i]=sc.nextInt();
}catch(Exceptione){
System.out.println("系統錯誤,您輸入的不為整數!");
System.exit(0);
}
}
if(arr[0]==arr[1]&&arr[0]==arr[2]){
System.out.println("這三個數相等");
}elseif(arr[0]+arr[1]+arr[2]>1000){
System.out.println("這三個數之和大於1000");
}else{
System.out.println("這三個數之和不大於1000");
}
}
}
㈦ JAVA作業
classaccount{
//賬戶余額
intmoney;
//利率
doublerate;
//構造函數,money為賬戶初始存款,rate為初始利率
publicaccount(intmoney,doublerate){
this.money=money;
this.rate=rate;
}
//向賬戶里存x元
publicvoiddeposit(intx){
money+=x;
}
//從賬戶里取x元
publicvoidwithdrawal(intx){
//取款
money-=x;
//如果透支則收10元罰款
if(money<0)
money-=10;
}
//每個月調用以更新賬戶內容,newRate為下個月的利率
publicvoipdate(intnewRate){
money=money*(1+rate);
rate=newRate;
}
}
㈧ Java if嵌套作業
換成這個
if(A==B && A==C ){
System.out.println("三數相等回");
}
else if(A+B+C>1000){
System.out.println("大於答1000");
}
else{
System.out.println("小於1000");
}
㈨ JAVA編程作業
importjava.math.BigDecimal;
importjava.util.Random;
publicclassIncrease{
publicstaticbooleanisPrime(inta){
=true;
if(a<2){//素數不小於2
returnfalse;
}else{
for(inti=2;i<=Math.sqrt(a);i++){
if(a%i==0){//若能被整除,則說明不是素數,返回false
flag=false;
break;//跳出循環
}
}
}
returnflag;
}
publicstaticvoidtest1(){
inti,n,k=0;
System.out.println("1-1000內素數");
for(n=3;n<=1000;n++){//3~1000的所有數
i=2;
while(i<n){
if(n%i==0)break;//若能整除說明n不是素數,跳出當前循環
i++;
}
if(i==n){//如果i==n則說明n不能被2~n-1整除,是素數
k++;//統計輸出數的個數
System.out.print(i+" ");
if(k%6==0)//每輸出5個則換行
System.out.println();
}
}
}
publicstaticvoidtest2()
{
Randomr=newRandom();
System.out.println(r.nextInt(301)-100);
}
publicstaticinttest3(intm,intn){
//輾轉相除法
intr;
do{
if(m<n)
{
r=m;
m=n;
n=r;
}
r=m%n;
m=n;
n=r;
}while(r!=0);
returnm;
}
publicstaticdoubletest4(intn){
doublee=1f;
doubletotal=1.0;
for(inti=0;i<n;i++)
{
total/=i+1;
e+=total;
}
BigDecimalb=newBigDecimal(e);
e=b.setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();//表明四捨五入,保留四位小數
returne;
}
publicstaticvoidmain(String[]args){
//第一題測試
System.out.println(isPrime(131));
test1();
//第二題測試
test2();
//第三題測試
System.out.println("最大公約數為:"+test3(1302,19924));
//第四題測試
System.out.println("e="+test4(100));
}
}
運行結果:
true
1-1000內素數
3 5 7 11 13 17
19 23 29 31 37 41
43 47 53 59 61 67
71 73 79 83 89 97
101 103 107 109 113 127
131 137 139 149 151 157
163 167 173 179 181 191
193 197 199 211 223 227
229 233 239 241 251 257
263 269 271 277 281 283
293 307 311 313 317 331
337 347 349 353 359 367
373 379 383 389 397 401
409 419 421 431 433 439
443 449 457 461 463 467
479 487 491 499 503 509
521 523 541 547 557 563
569 571 577 587 593 599
601 607 613 617 619 631
641 643 647 653 659 661
673 677 683 691 701 709
719 727 733 739 743 751
757 761 769 773 787 797
809 811 821 823 827 829
839 853 857 859 863 877
881 883 887 907 911 919
929 937 941 947 953 967
971 977 983 991 997 106
最大公約數為:2
e=2.7183
㈩ java作業
下面一道一道的完成:
第一題:
1)快速排序
package src;
/*
* 有幾個關鍵點
* 1 中間值到底取什麼
* 2 i和j的比較什麼時候需要是<=
* 3 遞歸調用的left和right值如何確定
*/
public class QuickSort { //快速排序
public static void main(String[] args) {
int[] a = {100,40, 60, 87, 34, 11, 56, 0};
int left = 0 ;
int right = a.length - 1;
quickSort(a , left , right);
//循環顯示數組
for(int i = 0 ; i < a.length ; i++){
System.out.print(a[i] + " ");
}
}
public static void quickSort(int[] a , int left , int right ){
int middle , temp ;
int i = left ;
int j = right ;
middle = a[left];
while(i < j){
while((a[i] < middle) && (i < right)){
i++;
}
while((a[j] > middle) && (j > left)){
j--;
}
if(i <= j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}
if(left < j){
quickSort(a , left , j);
}
if(right > i){
quickSort(a , i , right);
}
}
}
2)冒泡排序
package src;
public class PopSort {
public static void main(String[] args) {
int[] a = {100,40, 60, 87, 34, 11, 56, 0};
for(int i = 0 ; i < a.length ; i++)
for(int j = 0 ; j < a.length - i -1 ; j++){
if(a[j] > a[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
for(int k = 0 ; k < a.length ; k++){
System.out.print(a[k] + " ");
}
}
}
第二題
package src;
public class BinarySearch {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
int[] datas = { 1, 2, 3, 4, 5, 6 ,7, 8, 9, 10}; //二分法必須應用在有序數組
int key = 9;
System.out.println("the result is " + bSearch(datas , key));
}
public static int bSearch(int[] datas , int key){
int index = -1;
int low = 0;
int high = datas.length - 1;
while (low <= high) {
int middle = (low + high) >> 1;
if (key == datas[middle]) {
index = middle;
break;
} else if (key > datas[middle]) {
low = middle + 1;
} else if (key < datas[middle]) {
high = middle - 1;
}
}
return index;
}
}
第三題
package src;
public class CharCode {
public static void main(String[] args){
String str = "hello world";
char[] strToChar = str.toCharArray();
int charSum = 0;
for(int i = 0 ; i < strToChar.length ; i++){
charSum = charSum + strToChar[i];
}
System.out.print("the charArray's Sum is " + charSum);
}
}
第四題
package src;
public class NewArray {
/**
* @param args
*/
public static void main(String[] args) {
int a[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
int[] b = new int[a.length];
int j = 0;
for(int i = 0 ; i < a.length ; i++){
if(a[i] != 0){
b[j] = a[i];
j++;
}
}
for(int k = 0 ; k < j ; k++){
System.out.print(b[k] + " ");
}
}
}
第五題:
ArrayList實現
package src;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class StudentList {
/**
* @param args
*/
public String name;
public int age;
public String grade;
public String school;
public static List stuList = null;
public StudentList(String name, int age, String grade, String school) {
super();
this.name = name;
this.age = age;
this.grade = grade;
this.school = school;
}
public static void main(String[] args) {
stuList = new ArrayList();
stuList.add(new StudentList("aaa",13 , "6" ,"tianjin"));
stuList.add(new StudentList("kkk",13 , "6" ,"tianjin"));
stuList.add(new StudentList("ddd",13 , "6" ,"shenzhen"));
stuList.add(new StudentList("ccc",13 , "6" ,"beijing"));
stuList.add(new StudentList("eee",15 , "6" ,"tianjin"));
stuList.add(new StudentList("aaa",13 , "6" ,"tianjin"));
stuList.add(new StudentList("kkk",13 , "6" ,"tianjin"));
stuList.add(new StudentList("ddd",13 , "6" ,"shenzhen"));
stuList.add(new StudentList("ccc",13 , "6" ,"beijing"));
stuList.add(new StudentList("eee",15 , "6" ,"tianjin"));
for(Iterator it = stuList.listIterator() ; it.hasNext(); ){
StudentList stu = (StudentList)it.next();
System.out.println(stu.name + " ," +stu.age + " ," + stu.grade + " ," + stu.school );
}
}
}
數組實現:
package src;
public class StudentArray {
/**
* @param args
*/
public String name;
public int age;
public String grade;
public String school;
public StudentArray(String name, int age, String grade, String school) {
super();
this.name = name;
this.age = age;
this.grade = grade;
this.school = school;
}
public static void main(String[] args) {
StudentArray[] stua = {new StudentArray("aaa" ,13 ,"4" ,"beijing") , new StudentArray("bbb" ,14 ,"4" ,"dalian") ,new StudentArray("ddd" ,13 ,"4" ,"beijing") ,new StudentArray("ccc" ,13 ,"4" ,"tianjin") ,new StudentArray("eee" ,20 ,"8" ,"beijing") ,new StudentArray("fff" ,23 ,"10" ,"beijing") ,new StudentArray("aaa" ,13 ,"4" ,"beijing") ,new StudentArray("aaa" ,13 ,"4" ,"beijing") ,new StudentArray("kkk" ,13 ,"4" ,"beijing") ,new StudentArray("www" ,13 ,"4" ,"beijing") ,new StudentArray("aaa" ,13 ,"4" ,"beijing") };
for(int i = 0 ; i < stua.length ; i++){
System.out.println(stua[i].name + " , " + stua[i].age + " , " +stua[i].grade + " , " +stua[i].school);
}
}
}
第六題:
package job;
public abstract class Job { //這個是父類抽象類,表示職業類
public double baseWage; //基本工資
public Job(double baseWage) {
super();
this.baseWage = baseWage;
}
public double getBaseWage() {
return baseWage;
}
public void setBaseWage(double baseWage) {
this.baseWage = baseWage;
}
public double getYearWage(){ //一年的工資
return baseWage * 12 ;
}
}
package job;
public class Farmer extends Job {
public Farmer(double baseWage) {
super(baseWage);
// TODO 自動生成構造函數存根
}
public double getYearWage() {
// TODO 自動生成方法存根
return super.getYearWage();
}
}
package job;
public class Scientist extends Job {
public String yearendBonus; //年終獎
public Scientist(double baseWage) {
super(baseWage);
//
}
public String getYearendBonus() {
return yearendBonus;
}
public void setYearendBonus(String yearendBonus) {
this.yearendBonus = yearendBonus;
}
public double getYearWage() {
//
return super.getYearWage();
}
}
package job;
public class Teacher extends Job {
public double reward; //每天的課酬
public Teacher(double baseWage) {
super(baseWage);
// TODO 自動生成構造函數存根
}
public double getReward() {
return reward;
}
public void setReward(double reward) {
this.reward = reward;
}
public double getYearWage() {
// TODO 自動生成方法存根
return super.getYearWage();
}
}
package job;
public class Waiter extends Job {
public Waiter(double baseWage) {
super(baseWage);
// TODO 自動生成構造函數存根
}
public double getYearWage() {
// TODO 自動生成方法存根
return super.getYearWage();
}
}
package job;
public class Worker extends Job {
public Worker(double baseWage) {
super(baseWage);
// TODO 自動生成構造函數存根
}
public double getYearWage() {
// TODO 自動生成方法存根
return super.getYearWage();
}
}
package job;
public class Manager { //這是一個管理各種職業工資的類,這是一個含有具體業務的類
/**
* @param args
*/
public static void main(String[] args) {
Teacher tjob = new Teacher(5000); //教師的工資是5000美金一個月
printSalary(tjob);
Scientist sjob = new Scientist(8000);
printSalary(sjob);
Farmer fjob = new Farmer(4000);
printSalary(fjob);
Waiter wjob = new Waiter(3500);
printSalary(wjob);
Worker workjob = new Worker(4500);
printSalary(workjob);
}
public static void printSalary(Job job) {
System.out.println(job.getYearWage());
}
}
第七題:
package src;
public class Writer {
/**
* @param args
*/
public static void main(String[] args) {
int i = 911;
float j = 911.911f;
String str = "hello world";
write(i);
write(j);
write(str);
}
public static void write(int s) {
System.out.print("int: " + s + "\n");
}
public static void write(float s) {
System.out.print("float:" + s + "\n");
}
public static void write(String s) {
System.out.print("String:" + s + "\n");
}
}
好睏啊,睡覺了