『壹』 java編程作業,求打救,急.............................
import java.util.Random;
public class StudentsAward {
public static void main(String[] args) {
int[] id = {1,2,3,4,5,6,7,8,9,10};
boolean[] used = new boolean[id.length];
Random r = new Random();
int i ,j=0;
do{
i = r.nextInt(id.length);
if(used[i]){
continue;
}
j++;
System.out.println("三等獎獲得者是:"+id[i]+"號");
used[i] =true;
}while(j!=3);
do{
i = r.nextInt(id.length);
if(used[i]){
continue;
}
j++;
System.out.println("二等獎獲得者是:"+id[i]+"號");
used[i] =true;
}while(j!=5);
do{
i = r.nextInt(id.length);
if(used[i]){
continue;
}
j++;
System.out.println("一等獎獲得者是:"+id[i]+"號");
used[i] =true;
}while(j!=6);
}
}
『貳』 java編程作業
public class Customer {
private String memberName;
private int memberNo;
public Customer(String memberName,int memberNo){
this.memberName = memberName;
this.memberNo = memberNo;
}
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
public int getMemberNo() {
return memberNo;
}
public void setMemberNo(int memberNo) {
this.memberNo = memberNo;
}
}
----------------------------------分界線---------------------------------------------------
import java.util.ArrayList;
public class CustManager {
private static ArrayList<Customer> list = new ArrayList<Customer>();
public void addCust(String memberName,int memberNo){
list.add(new Customer(memberName, memberNo));
}
public int searchMemberByName(String memberName){
if(memberName == null || memberName.equals("")){
return 1;
}
for(Customer c : list){
if(memberName.equals(c.getMemberName())){
return c.getMemberNo();
}
}
return 0;
}
}
----------------------------------分界線---------------------------------------------------
public class Test {
public static void main(String[] args) {
CustManager custManager = new CustManager();
custManager.addCust("張三", 1);
custManager.addCust("李四", 2);
custManager.addCust("王五", 3);
custManager.addCust("馬六", 4);
System.out.println(custManager.searchMemberByName("馬六"));
}
}
『叄』 java編程的作業試題大家幫我解答一下,謝謝了,一共兩個題,在下面,請大家一定要幫我解答一下,我不會做
僅供參考,未測試
Question1
packagecom.kidd.atmtest;
importjava.util.Scanner;
publicclassQuestion1{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
System.out.print("輸入基本運費,貨重,距離(均為整數,用,分割):");
String[]ss=scanner.next().split(",");
intp=Integer.parseInt(ss[0]);
intw=Integer.parseInt(ss[1]);
ints=Integer.parseInt(ss[2]);
doubled;
if(s<250){
d=0;
}elseif(s<500){
d=0.02;
}elseif(s<1000){
d=0.05;
}elseif(s<2000){
d=0.08;
}else{
d=0.1;
}
System.out.println("總運費f="+(p*w*s*(1-d)));
}
}
Question2
packagecom.kidd.atmtest;
importjava.util.Scanner;
publicclassQuestion2{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
System.out.print("請輸入x:");
intx=scanner.nextInt();
inty;
if(x<0){
y=-1;
}elseif(x>0){
y=1;
}else{
y=0;
}
System.out.println("y="+y);
}
}
Question3
packagecom.kidd.atmtest;
importjava.util.Scanner;
publicclassQuestion3{
publicstaticvoidmain(String[]args){
intindex=0;
Scannerscanner=newScanner(System.in);
booleanend=false;
while(!end){
System.out.print("請輸入卡號,密碼:");
scanner.next();
index++;
System.out.print("賬號或密碼錯誤次數"+index+",");
if(index>=3){
System.out.println("吞卡");
end=true;
}else{
System.out.println("請重新輸入.");
}
}
}
}
Question4
packagecom.kidd.atmtest;
importjava.util.Scanner;
publicclassQuestion4{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
System.out.print("輸入一個整數:");
Stringstring=scanner.next();
for(inti=0,k=string.length();i<k;i++){
System.out.println(string.charAt(i));
}
}
}
Question5
packagecom.kidd.atmtest;
importjava.util.Random;
importjava.util.Scanner;
publicclassQuestion5{
publicstaticvoidmain(String[]args){
inti=newRandom().nextInt(40)+60;
booleanend=false;
intn;
intindex=1;
Scannerscanner=newScanner(System.in);
while(!end){
System.out.print("請輸入你猜的結果:");
n=scanner.nextInt();
if(n>i){
System.out.println("大了");
index++;
}elseif(n<i){
System.out.println("小了");
index++;
}else{
end=true;
}
}
System.out.print("猜對了,一共猜了"+index+"次,你是");
if(index<5){
System.out.print("天才.");
}else{
System.out.print("笨蛋.");
}
}
}
Question6
packagecom.kidd.atmtest;
importjava.util.Scanner;
publicclassQuestion6{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
System.out.print("請輸入要列印的元素個數:");
intn=scanner.nextInt();
if(n<1){
System.out.println("無輸出結果");
}
if(n>=1){
System.out.print("1");
}
if(n>=2){
System.out.print(",2");
}
if(n>2){
intf1=1;
intf2=2;
intsum;
for(inti=3;i<=n;i++){
sum=f1+f2;
System.out.print(","+(f1+f2));
f1=f2;
f2=sum;
}
}
}
}
『肆』 Java編程作業
public static void main(String[] args) {
System.out.println("請輸入內容:");
while (true) {
Scanner s = new Scanner(System.in);
String str = s.next();//原始字元串
if(str.equals("!")){
//System.out.println("請重新輸入內容:");//繼續用這個
break;//退出用這個
}else{
String afterAtr="";//修改後
int num = 0;//修改數量
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isLowerCase(c)) {
afterAtr += Character.toUpperCase(c);
num++;
}else if (Character.isUpperCase(c)) {
afterAtr += Character.toLowerCase(c);
num++;
}else{
afterAtr += c;
}
}
System.out.println("原始內容:"+str+";修改後內容:"+afterAtr+";修改數量:"+num);
}
}
}
『伍』 JAVA編程作業
importjava.math.BigDecimal;
importjava.util.Random;
publicclassIncrease{
publicstaticbooleanisPrime(inta){
booleanflag=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編程作業:構造方法的重載
我是初學者,我把我寫的給你看看,我們可以一起學習交流的啊~
class Point{
double x,y;
Point(double x,double y){
this.x = x;
this.y = y;
}
double getX(){
return x;
}
double getY(){
return y;
}
}
class Circle{
double r;
Point pp;
Circle(double r){
this.r = r;
pp =new Point (0,0);
}
Circle(double r,Point p ){
this.r = r;
this.pp=p;
}
boolean isContain(Point pt){
double a =(pp.x-pt.x)*(pp.x-pt.x)+(pp.y-pt.y)*(pp.y-pt.y);
double b = this.r*this.r;
if(a<b){
return true;
}else{
return false;
}
}
}
public class TestOverLoad{
public static void main (String[] args){
Circle c1=new Circle(1);
Point p=new Point(1,1);
System.out.println(c1.isContain(p));
}
}
『柒』 Java編程作業,急用
以下是源代碼和運行結果截圖,如果哪裡有問題的話可以來找我。
classPet{
intage;
intweight;
publicPet(intage,intweight){
super();
this.age=age;
this.weight=weight;
}
publicvoidshowInfo(){
System.out.println("myweightis"+this.weight+"andmyageis"+this.age);
}
publicintgetWeightInfo(){
returnthis.weight;
}
publicintgetAgeInfo(){
returnthis.age;
}
}
interfaceEatable{
publicabstractvoidbeEatted();//默認為public和abstract
}
classCatextendsPet{
publicCat(intage,intweight){
super(age,weight);
//TODOAuto-generatedconstructorstub
}
privateintcry;
publicvoidshowInfo(){
System.out.println("I'maCat,myweightis"+this.weight+"andmyageis"+this.age);
}
publicintgetWeightInfo(){
returnsuper.getWeightInfo();
}
publicintgetAgeInfo(){
returnsuper.getAgeInfo();
}
}
{
publicDog(intage,intweight){
super(age,weight);
//TODOAuto-generatedconstructorstub
}
privateintcry;
publicvoidshowInfo(){
System.out.println("I'maDog,myweightis"+this.weight+"andmyageis"+this.age);
}
publicintgetWeightInfo(){
returnsuper.getWeightInfo();
}
publicintgetAgeInfo(){
returnsuper.getAgeInfo();
}
publicvoidbeEatted(){
}
}
classFruitimplementsEatable{
publicFruit(Stringcolor,Stringaddress){
super();
this.color=color;
this.address=address;
}
Stringcolor;
Stringaddress;
publicvoidbeEatted(){
System.out.println("I'mbeeneaten!");
}
publicvoidshowInfo(){
System.out.println("mycoloris"+this.color+"andmyaddressis"+this.address);
}
publicStringgetColorInfo(){
returnthis.color;
}
publicStringgetAddressInfo(){
returnthis.address;
}
}
classBananaextendsFruit{
publicBanana(Stringcolor,Stringaddress){
super(color,address);
//TODOAuto-generatedconstructorstub
}
publicvoidbeEatted(){
System.out.println("I'maBanana,I'mbeeneaten!");
}
publicvoidshowInfo(){
System.out.println("I'maBanana,mycoloris"+this.color+"andmyaddressis"+this.address);
}
@Override
publicStringgetColorInfo(){
//TODOAuto-generatedmethodstub
returnthis.getColorInfo();
}
@Override
publicStringgetAddressInfo(){
returnthis.getAddressInfo();
}
}
classAppleextendsFruit{
Stringtype;
publicApple(Stringcolor,Stringaddress,Stringtype){
super(color,address);
this.type=type;
//TODOAuto-generatedconstructorstub
}
publicStringgetType(){
returntype;
}
publicvoidbeEatted(){
System.out.println("I'manapple,I'mbeeneaten!");
}
publicvoidshowInfo(){
System.out.println("I'manApple,mycoloris"+this.color+",myaddressis"+this.address
+"andmytypeis"+type);
}
@Override
publicStringgetColorInfo(){
//TODOAuto-generatedmethodstub
returnthis.getColorInfo();
}
@Override
publicStringgetAddressInfo(){
returnthis.getAddressInfo();
}
}
publicclassPrograms{
publicstaticvoidmain(String[]args){
Catmycat=newCat(10,20);
Dogmydog=newDog(10,24);
Bananamybanana=newBanana("黃色","北京");
Applemyapple=newApple("紅色","上海","新品種");
mycat.showInfo();
mydog.showInfo();
mybanana.showInfo();
myapple.showInfo();
mydog.beEatted();
mybanana.beEatted();
myapple.beEatted();
}
}