1:下列哪个答案与show不是方法重载( B )。
class Demo {
void show(int a,int b,float c){}
}
A.void show(inta,float c,int b){}
B,void show(int x,inty,float z){}
C.int show(int a,floatc,int b){return a;}
D.int show(int a,floatc){return a;}
2:下面程序的结果是( C )。
class Demo {
public static void main(String[] args){
int[] a = { 1, 2, 3, 4 };
int[] b = a;
b[2] = 100;
System.out.println(a[2]);
}
}
A:编译失败
B:2
C:100
D:3
package practice2;
//定义一个数组,并把数组元素遍历出来
//定义一个数组,并把数组元素遍历出来
public class demo1 {
public static void main(String[] args) {
//定义一个数组arr
int[]arr={1,2,3,4};
//调用方法arrayShow
arrayShow(arr);
}public static void arrayShow(int[]arr){
//用for语句将arr数组中的每个元素输出
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
public static void main(String[] args) {
//定义一个数组arr
int[]arr={1,2,3,4};
//调用方法arrayShow
arrayShow(arr);
}public static void arrayShow(int[]arr){
//用for语句将arr数组中的每个元素输出
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
package practice2;
//定义一个数组,找出数组中的最大值
//定义一个数组,找出数组中的最大值
public class demo2 {
public static void main(String[] args) {
//创建一个数组arr
int[]arr= {1,2,3,4,5};
//调用方法getMax
getMax(arr);
}public static void getMax(int[]arr){
//假设arr[0]就是max
int max=arr[0];
for (int i = 0; i < arr.length; i++) {
//用arr数组中的每个元素与max进行比较,将大的赋值给max
if (arr[i]>max) {
max=arr[i];
}
}
//输出max必须在for语句之外否则会输出所有数组值
System.out.println(max);
}
public static void main(String[] args) {
//创建一个数组arr
int[]arr= {1,2,3,4,5};
//调用方法getMax
getMax(arr);
}public static void getMax(int[]arr){
//假设arr[0]就是max
int max=arr[0];
for (int i = 0; i < arr.length; i++) {
//用arr数组中的每个元素与max进行比较,将大的赋值给max
if (arr[i]>max) {
max=arr[i];
}
}
//输出max必须在for语句之外否则会输出所有数组值
System.out.println(max);
}
}
package practice2;
//定义一个数组,把数组反向打印
//定义一个数组,把数组反向打印
public class demo3 {
public static void main(String[] args) {
int[]arr={1,2,3,4,5};
for (int i = 0; i < arr.length; i++) {
arr[i]=i+1;
}
public static void main(String[] args) {
int[]arr={1,2,3,4,5};
for (int i = 0; i < arr.length; i++) {
arr[i]=i+1;
}
for (int j=0; j <= arr.length; j++) {
System.out.print(arr[j]+" ");
}
System.out.print(arr[j]+" ");
}
}
}
}
package practice2;
//定义一个数组,查找元素在数组中第一次出现的位置
//定义一个数组,查找元素在数组中第一次出现的位置
public class demo4 {
public static void main(String[] args) {
//定义一个数组
int[]arr={1,2,3,4};
//输入要找的元素num
int num=3;
public static void main(String[] args) {
//定义一个数组
int[]arr={1,2,3,4};
//输入要找的元素num
int num=3;
for (int i = 0; i < arr.length; i++) {
//用num与数组中的元素进行比较,输出相等的元素索引
if (num==arr[i]){
System.out.println("arr["+i+"]");
}
}
}
//用num与数组中的元素进行比较,输出相等的元素索引
if (num==arr[i]){
System.out.println("arr["+i+"]");
}
}
}
}
package practice;
//学生的成员变量:姓名,年龄,学号
//成员方法:学习,睡觉,吃饭
//学生的成员变量:姓名,年龄,学号
//成员方法:学习,睡觉,吃饭
public class student {
String name;
int age;
int num;
public void study(){
System.out.println("学生要学习");
}public void sleep(){
System.out.println("学生要睡觉");
}public void eat(){
System.out.println("学生要吃饭");
}
String name;
int age;
int num;
public void study(){
System.out.println("学生要学习");
}public void sleep(){
System.out.println("学生要睡觉");
}public void eat(){
System.out.println("学生要吃饭");
}
}
package practice;
public class test {
public static void main(String[] args) {
//创建一个学生对象
//类名 对象名=new 对象名
student s=new student();
//给学生录入信息
s.name="貂蝉";
s.age=20;
s.num=1234;
System.out.println(s.name);
System.out.println(s.age);
System.out.println(s.num);
//使用学生成员方法
s.study();
s.sleep();
s.eat();
}
public static void main(String[] args) {
//创建一个学生对象
//类名 对象名=new 对象名
student s=new student();
//给学生录入信息
s.name="貂蝉";
s.age=20;
s.num=1234;
System.out.println(s.name);
System.out.println(s.age);
System.out.println(s.num);
//使用学生成员方法
s.study();
s.sleep();
s.eat();
}
}
package practice1;
public class phone {
//手机的成员变量:颜色,价格,品牌
//成员方法:打电话,发短信,玩游戏
String color;
int price;
String brand;
public void call(){
System.out.println("手机可以打电话");
}public void message(){
System.out.println("手机可以发短信");
}public void playGame(){
System.out.println("手机可以玩游戏");
}
//手机的成员变量:颜色,价格,品牌
//成员方法:打电话,发短信,玩游戏
String color;
int price;
String brand;
public void call(){
System.out.println("手机可以打电话");
}public void message(){
System.out.println("手机可以发短信");
}public void playGame(){
System.out.println("手机可以玩游戏");
}
}
package practice1;
public class test {
public static void main(String[] args) {
//创建一个手机对象
//类名 对象名=new 对象名
phone p=new phone();
//给手机录入信息
p.color="金色";
p.brand="华为";
p.price=3000;
System.out.println(p.color);
System.out.println(p.brand);
System.out.println(p.price);
//使用手机成员方法
p.call();
p.message();
p.playGame();
}
public static void main(String[] args) {
//创建一个手机对象
//类名 对象名=new 对象名
phone p=new phone();
//给手机录入信息
p.color="金色";
p.brand="华为";
p.price=3000;
System.out.println(p.color);
System.out.println(p.brand);
System.out.println(p.price);
//使用手机成员方法
p.call();
p.message();
p.playGame();
}
}