數(shù)組、變量、類和對象、循環(huán),練習題總結(jié)
練習2
1.
class Happy {
public static void main(String args[]) {
int i = 1 ;
int j = i++ ;
if((i==(++j))&&((i++)==j)) {
i += j ;
}
System.out.println("i = "+i);
}
}
運行完上面代碼之后輸出i的值是多少?
A. 4
B. 5
C. 3
D. 6
2. 下面的數(shù)據(jù)聲明及賦值那一個是沒有錯誤的?
A. float f = 1.3;
B. char c = "a";
C. byte b = 257;
D. int i = 10;
3. 編譯Java源程序文件產(chǎn)生的字節(jié)碼文件的擴展名為?
A. java
B. class
C. html
D. exe
4. 現(xiàn)在假設(shè)有如下程序:
public class Demo {
public static void main(String args[]) {
boolean flag = 10%2 == 1 && 10 / 3 == 0 && 1 / 0 == 0 ;
System.out.println(flag ? "aliyunedu" : "yootk") ;
}
}
以上程序的最終執(zhí)行結(jié)果是什么?
A. aliyunedu
B. yootk
C. true
D. 程序出錯
5. 現(xiàn)在假設(shè)有如下程序:
public class Demo {
public static void main(String args[]) {
int x = 10 ;
double y = 20.2 ;
long z = 10L;
String str = "" + x + y * z ;
System.out.println(str) ;
}
}
以上程序的最終執(zhí)行結(jié)果是什么?
A. 10202.0
B. 0212.0
C. 302.0
D. 1020.210
6. 現(xiàn)在假設(shè)有如下程序:
public class Demo {
public static void main(String args[]) {
String str = "" ;
for (int x = 0 ; x < 5 ; x ++) {
str += x ;
}
System.out.println(str) ;
}
}
以上程序最終的執(zhí)行結(jié)果是什么?
A. 01234
B. 10
C. 14
D. 25
7. 現(xiàn)在假設(shè)有如下程序:
public class Demo {
public static void main(String args[]) {
System.out.println(inc(10) + inc(8) + inc(-10)) ;
}
public static int inc(int temp) {
if (temp > 0) {
return temp * 2 ;
}
return -1 ;
}
}
以上程序的最終執(zhí)行結(jié)果是什么?
A. 35
B. 8
C. 28
D. 12
8. 現(xiàn)在假設(shè)有如下程序:
public class Demo {
public static void main(String args[]) {
char c = 'A' ;
int num = 10 ;
switch(c) {
case 'B' :
num ++ ;
case 'A' :
num ++ ;
case 'Y' :
num ++ ;
break ;
default :
num -- ;
}
System.out.println(num) ;
}
}
以上程序的最終執(zhí)行結(jié)果是什么?
A. 11
B. 13
C. 12
D. 10
9. 現(xiàn)在假設(shè)有如下程序:
public class Demo {
public static void main(String args[]) {
int sum = 0 ;
for (int x = 1 ; x < 10 ; x ++) {
sum += x ;
if (x % 3 == 0) {
continue ;
}
}
System.out.println(sum) ;
}
}
以上程序的最終執(zhí)行結(jié)果是什么?
A. 6
B. 0
C. 程序錯誤,死循環(huán)
D. 45
10. 現(xiàn)在假設(shè)有如下程序:
public class Demo {
public static void main(String args[]) {
int sum = 0 ;
for (int x = 0 ; x < 10 ; x ++) {
sum += x ;
if (x % 3 == 0) {
break ;
}
}
System.out.println(sum) ;
}
}
以上程序的最終執(zhí)行結(jié)果是什么?
A. 6
B. 0
C. 程序錯誤,死循環(huán)
D. 45
11.數(shù)組的最大值
public class Per{
public static void main(String[] args){
int[] a = {15,62,95,26,523,526,521,549,999};
int max = a[0];
for(int i = 1;i < a.length;i++){
if(a[i] > max){
max = arr[i];
}
}
System.out.print(max);
}
}
練習題1
1,
public class Test {
public static void main(String[] args) {
int i = 1,j;
j = ++i + i++ + ++i + ++i + i++;
System.out.println(j);
}
}
2,
public class Test2 {
public static void main(String[] args) {
int i = 1;
i+= ++i;
System.out.println(i);
}
}
3,
import java.util.Scanner;
public class Test3<z> {
public static void main(String[] args) {
System.out.println("今天是星期幾?");
Scanner day = new Scanner(System.in);
int days = day.nextInt();
String z = (days > 5 && days <= 7) ? "今天是周末" : "今天是工作日";
System.out.println(z);
}
}
4,
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
System.out.println("請輸入一個月份: ");
Scanner input = new Scanner(System.in);
int month = input.nextInt();
switch(month){
case 3:
case 4:
case 5:
System.out.print("是春季");
return;
case 6:
case 7:
case 8:
System.out.print("是夏季");
return;
case 9:
case 10:
case 11:
System.out.print("是秋季");
return;
case 1:
case 2:
case 12:
System.out.print("是冬季");
return;
}
}
}
5,
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("請輸入一個整數(shù):");
int N = s.nextInt();
int a = 1;
while (N > 0) {
a *= N;
N--;
}
System.out.println("階乘為:" + a);
}
}
6,
public class Test6 {
public static void main(String[] args) {
int s = 0;
int m = 1;
for(int i = 1;i <= 10;i++){
s = s + m;
m = m * 2;
}
System.out.println("乞丐十天的收入是: " + s);
}
}
7,
public class Test7 {
public static void main(String[] args) {
int i;
for (i = 1;i <= 100;i++) {
if (i % 3 != 0 && i % 5 != 0){
System.out.println(i);
}
}
}
}
8,
public class TTT {
public static void main(String[] args) {
int[] a = new int[5];
System.out.println("數(shù)組的隨機數(shù)為:");
for (int i = 0; i < 5; i++) {
a[i] = (int) (Math.random() * 100);
System.out.print(a[i]+" ");
}
for (int i = 0; i < a.length /2; i++) {
int temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}
System.out.println();
System.out.println("-----Reversed Array------");
for (int c: a ) {
System.out.print(c+" ");
}
}
}
練習3
1,統(tǒng)計出 1 到1000 之間有多少個能被3整除的數(shù)
public class Aa{
public static void main(String[] args)
{
for(int i = 1;i <= 1000;i++){
if(i % 3 ==0){
System.out.println(i);
}
}
}
}
2,用switch...case實現(xiàn):給出一百分制成績,要求輸出成績等級’A’,’B’,’C’,’D’,’E’。90 分以上為’A’,80~89 分為’B’,70~79 分為’C’,60~69 分為’D’,60分以下為’E’
import java.util.Scanner;
public class Value{
public static void main(String[] args){
System.out.print("請輸入一個成績: ");
Scanner input = new Scanner(System.in);
int s = input.nextInt();
switch(s / 10){
case 9:
System.out.print('A');
break;
case 8:
System.out.print('B');
break;
case 7:
System.out.print('C');
break;
case 6:
System.out.print('D');
break;
default:
System.out.print('E');
break;
}
}
}
3,求一個數(shù)的絕對值
public class Aaaaa{
public static void main(String[] args){
int a=-6;
if(a>=0){
System.out.println(a);
}else{
System.out.println(-a);
}
}
}
4,
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
float money=0,tax=0,old=0,medicare=0,outWork=0,house=0;
System.out.print("請輸入應(yīng)發(fā)工資:");
money=in.nextFloat();
old=(float)(money*0.08);//計算養(yǎng)老
medicare=(float)(money*0.02);//計算醫(yī)保
outWork=(float)(money*0.002);//計算失業(yè)
house=money*0.12f;//計算住房公積金
money=money-(old+medicare+outWork+house);
}
5,編寫程序,從鍵盤輸入一個0~99999之間的任意數(shù),判斷輸入的數(shù)是幾位數(shù)?
import java.util.Scanner;
public class Aaaa{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("請輸入一個0~99999之間的整數(shù): ");
int a = input.nextInt();
if(a / 10000 > 1 && a / 10000 < 10){
System.out.print("是5位的整數(shù)");
}else if(a / 1000 >=1){
System.out.print("是4位的整數(shù)");
}else if(a / 100 >=1){
System.out.print("是3位的整數(shù)");
}else if(a / 10 >=1){
System.out.print("是2位的整數(shù)");
}
}
}
6,編寫程序,對輸入的一個整數(shù),按相反順序輸出該數(shù)。
public class Aaaaaaa{
public static void main(String[] args) {
int num = 123;
while (num>0) {
int a = num % 10;
num /= 10;
System.out.print(a);
}
}
}
7,一個數(shù)如果恰好等于它的因子之和,這個數(shù)就稱為“完數(shù)”。例如6=1+2+3.編程找出1000以內(nèi)的所有完數(shù)。
#include <stdio.h>
void main()
{
int i,j,sum;
for(i=3;i<=1000;i++)
{
sum=1;
for(j=2;j<i;j++)
{
if(i%j==0)
{
sum+=j;
}
}
if(sum==i)
{
printf("%d\n",sum);
}
}
}
8,輸入兩個正整數(shù)m和n,求其最大公約數(shù)。9,輸入兩個正整數(shù)m和n,求其最小公倍數(shù)。
不懂
package mytest;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
System.out.print("請輸入正整數(shù) m 的值:");
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
System.out.print("請輸入正整數(shù) n 的值:");
Scanner scanner1 = new Scanner(System.in);
int n = scanner1.nextInt();
Example example = new Example();
int a = example.division(m,n);
int b = m*n/a;
System.out.println(m+"和"+n+"的最大公約數(shù)為:"+a+",最小公倍數(shù)為:"+b);
}
public int division(int x,int y){
int t;
if(x<y){//交換位置
t=x;
x=y;
y=t;
}
while (y!=0){
if(x==y){
return 1;
}else {
int k = x % y;
x=y;
y=k;
}
}
return x;
}
}
10,有1.2.3.4個數(shù)字,能組成多少個互相不相同且無重復(fù)數(shù)字的三位數(shù)?都是多少?
public class Aaaaaa{
public static void main(String[] args) {
int count = 0;
for( int i = 1 ; i < 5 ; i++ ) {
for( int j = 1 ; j < 5 ; j++ ) {
for( int k = 1 ; k < 5 ; k++ ) {
if( i!=j && j!=k && i!=k) {
count++;
System.out.println(i+""+j+""+k);
}
}
}
}
System.out.println("共有"+count+"個不重復(fù)的三位數(shù)");
}
}
11,有一對兔子,從出生后第三個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數(shù)為多少?
public class rabbit {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“請輸入月份:”);
int Inputmonth = sc.nextInt();
int i = 1, j = 0, month, x;
for (month = 1; month < Inputmonth; month++) {
x = i;
i = i + j;
j = x;
}
System.out.println(“第” + month + “個月有” + 2 * i + “只兔子?!?;
}
1.類和對象的不同之處是什么
類是抽象的,不是具體的,對象是具體的實例
2.如何定義類
修飾符 class關(guān)鍵字 類名{}
3.每個對象都有自己的什么副本
實例變量副本
4.使用兩條單獨的語句,寫出如何聲明一個counter的Mycounter的對象
Mycounter counter = new Mycounter();
5.寫出如何聲明一個名為myMeth()的方法,該方法有double返回類型和兩個名為a和b的int類型的形參
double myMeth(int a,int b){}
6.如果方法返回一個值,那么必須如何返回
使用return
7.構(gòu)造函數(shù)的名稱是什么
類名
8.new的作用是什么
可用于創(chuàng)建任何類型的對象,返回對新創(chuàng)建的對象的引用
9.this的作用是什么
引用實例變量
10.構(gòu)造函數(shù)可以有一個或多個形參嗎
可以
11.如果方法不返回值,那么返回類型必須是什么
void
12.類和對象存在可以解決什么問題
13.類和對象在體系里是什么邏輯
14.類的基本格式,都包括了什么
修飾符 class關(guān)鍵字 類名{
成員變量
成員方法
}
15.創(chuàng)建對象時都發(fā)生什么,執(zhí)行什么
類名 對象名 = new 類名();
對象的類文件加載進內(nèi)存
靜態(tài)成員,方法被調(diào)用才會執(zhí)行
靜態(tài)代碼塊優(yōu)先于對象給類初始化
對屬性進行顯式初始化
構(gòu)造代碼塊初始化,給對象初始化
對對象進行相應(yīng)的構(gòu)造函數(shù)初始化
將內(nèi)存地址賦值給棧內(nèi)存的變量
16。使用對象的成員變量和成員方法
對象名后加.運算符可以調(diào)用
17.方法返回值 如何使用方法返回值
返回值可以用于不同的目的,在某些情況下,返回值也包含一些計算的結(jié)果,在另一些情況下,返回值只用來顯示成功或失敗
使用return
18.方法的參數(shù) 形參和實參
向方法傳遞的值稱為實參,負責接收實參的變量叫做形參
19.構(gòu)造函數(shù)
構(gòu)造函數(shù)就是在創(chuàng)建對象時初始化的對象,構(gòu)造函數(shù)通常用來初始化類定義的實例變量,或執(zhí)行其他創(chuàng)建完整對象所需要的啟動過程,如果沒有定義構(gòu)造函數(shù),JAVA自動提供了默認的構(gòu)造函數(shù),將所有成員變量初始化為它們的默認值
20.構(gòu)造方法和普通方法的區(qū)別
普通方法
是有修飾符修飾的成員方法,根據(jù)關(guān)鍵字static的有無分為靜態(tài)方法和非靜態(tài)方法;一旦使用static修飾成員方法,就成為了靜態(tài)方法,靜態(tài)方法不屬于對象,而是屬于類的;如果沒有static修飾的成員方法,那么必須先創(chuàng)建對象,然后通過對象調(diào)用它;普通方法可以有返回值也可以沒有返回值,而構(gòu)造方法不能有返回值;普通方法是不能通過new來創(chuàng)建的,可以通過對象名來調(diào)用;
構(gòu)造方法
當一個類實例化對象的時候,用到的方法就是構(gòu)造方法,我們可以看到在一個類里面的構(gòu)造方法并不是從別的類里面引進來的,而是自己本身就有的方法。換句話說,構(gòu)造方法就是類構(gòu)造對象時調(diào)用的方法,主要用來實例化對象。
構(gòu)造方法:對象創(chuàng)建時,就會調(diào)用與之對應(yīng)的構(gòu)造方法,對對象進行初始化;在對象創(chuàng)建時,會調(diào)用且只調(diào)用一次。
普通方法:對象創(chuàng)建后,需要方法功能時才會調(diào)用。對象創(chuàng)建后,可以被調(diào)用多次。
21.this關(guān)鍵字如何使用,何時使用
用來調(diào)用與成員變量重名的實例變量