
正文
java写代码变量例题 java变量名的命名规则
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA的几道题目
1.
public class ThreeNumber {
public static void main(String agrs[]) {
int t=0;
for (int i=100; i=1000;i++) {
if (i%3 == 0) {
System.out.printf("%4d",i);
t++;
if(t%6==0)
System.out.println(""); //每行打印6个数
}
}
}
}
2.
public class CiFang {
public static void main(String agrs[]) {
System.out.println(find2(40));
}
static double find2 (int n) {
return Math.pow(2, n);
}
}
3.
public class ShowCar {
public static void main(String[] args) {
Car bentian = new Car("本田","黑色",1500,5);
System.out.println("品牌:"+bentian.name);
System.out.println("颜色:"+bentian.color);
System.out.println("自重:"+bentian.weight+"公斤");
System.out.println("搭乘人数:"+bentian.passenger);
}
}
class Car {
String name;
String color;
double weight;
int passenger;
public Car (String name,String color,double weight,int passenger) {
this.name = name;
this.color = color;
this.weight = weight;
this.passenger = passenger;
}
}
4.
public class Array {
public static void main(String[] args) {
int []number = {5,6,1,7,3};
int min = number[0],max =number[0],sum = number[0];
for(int i=1;inumber.length;i++) {
sum = sum +number[i];
if (number[i]min)
min = number[i];
if(number[i]max)
max = number[i];
}
System.out.println("数组元素和:"+sum);
System.out.println("数组最小元素:"+min);
System.out.println("数组最大元素:"+max);
}
}
5.
import java.io.*;
public class TestString {
private String input;
String[] strs = new String[10]; //根据输入生成的字符串数组
private static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter stdOut = new PrintWriter(System.out,true);
public void inputString() throws IOException{
stdOut.println("请输入10个以内的字符串,输入exit结束输入:");
int i = 0;
while((input = stdIn.readLine())!=null){
String[] temp = new String[i+1];
if(input.equals("exit"))
break;
strs[i] = input;
i++;
}
for(int k = 0;ki;k++)
System.out.println(strs[k]);
}
public static void main(String[] args){
try {
new Homework().inputString();
} catch (IOException e) {
e.printStackTrace();
}
}
}
最后一个程序可能不是很完善,最多只能输入十个字符串!
相关问答
Q1: 在Java中定义一个常变量,该常变量用来代替利率,利率为0.45236; 请问完整的代码怎么写?
float irate=0.0000000f; //定义一个变量 irate 并初始化 利率这个变量名其实可以随意取 如果没有规范java写代码变量例题的话 。注意不初始化 下一步赋值可能会报错java写代码变量例题的喔 或者irate=0.0f也行
irate=0.45236java写代码变量例题; //给irate 赋值
Q2: 求java代码 。题目是,创建一个Person类,成员变量为姓名,性别,年龄,使用键盘赋值,并显
public class Person {
String name;
String sex;
int age;
public static void main(String[]args) throws IOException{
Person p1=new Person();
Person p2=new Person();
//输入p1的信息
System.out.println("请输入姓名:");
p1.name=System.in.read();
System.out.println("请输入性别:");
while(1)
{
p1.sex=System.in.read();
if(p1.sex=="man")
break;
else
System.out.println("性别输入有误,请重新输入!");
}
System.out.println("请输入年龄:");
while(1)
{
p1.age=System.in.read();
if(p1.age=0p1.age=120)
break;
else
System.out.println("年龄输入有误,请重新输入!");
}
//输出第一个人的信息
System.out.println("第一个人的姓名:"+p1.name+"\n性别"+p1.sex+"\n年龄:"+p1.age);
//输入p2的信息
System.out.println("请输入姓名:");
p2.name=System.in.read();
System.out.println("请输入性别:");
while(1)
{
p2.sex=System.in.read();
if(p2.sex=="female")
break;
else
System.out.println("性别输入有误,请重新输入!");
}
System.out.println("请输入年龄:");
while(1)
{
p2.age=System.in.read();
if(p2.age=0p2.age=120)
break;
else
System.out.println("年龄输入有误,请重新输入!");
}
//输出第二个人的信息
System.out.println("第二个人的姓名:"+p2.name+"\n性别"+p2.sex+"\n年龄:"+p2.age);
}
}
Q3: java作业题,都在这里了,希望你帮忙解答下
1选择题
1,B
2.D
3.D
2.int i = 30;
double d[] = new double[30];
char[] r = new char[30];
int[] i = {3,4,5,6};
float f[] ={2.3F,3.5F,6.6F};
char[] c = new char[3];
3. int类型java写代码变量例题,最小为0java写代码变量例题,a[2]
4. double[] d = new double[10];
d[9] = 5.5D;
double result = d[0] + d[1];
5. public static void method1(int n, int m) {
n += m;
method2((int) 3.4);
}
public static int method2(int n) {
if (n 0)
return 1;
else if (n == 0)
return 0;
else
return -1;
}
6.
public double getPromotion(double amount, double percent)
public void print(int year, int month)
public double sqrt(double d)
public boolean isOdd(int i);
public void printMsg(int times);
public double getLoanMonthlyPayment(double totalMoney, int loanYears, double interest);
public Char toUpperCase(char c);
public static void question1() {
Scanner scanner = new Scanner(System.in);
double[] scores = new double[10];
int i = 0;
double totalScore = 0;
while (i 10) {
double d = scanner.nextDouble();
scores[i] = d;
i++;
totalScore += d;
}
double avgScore = totalScore / 10;
Arrays.sort(scores);
int index = Arrays.binarySearch(scores, avgScore);
System.out.println("低于平均成绩java写代码变量例题的" + (index));
System.out.println("高于平局成绩java写代码变量例题的" + (10 - index));
scanner.close();
}
public static void question2() {
double[] scores = { 10.0, 9.0, 8.0, 10.0, 7.0, 10.0, 9.0, 8.0, 10.0, 7.0 };
Arrays.sort(scores);
double[] newScores = new double[8];
for (int i = 1; i scores.length - 2; i++) {
newScores[i - 1] = scores[i];
}
double totalScore = 0;
for (int i = 0; i newScores.length; i++) {
totalScore += newScores[i];
}
System.out.println("得分是" + totalScore);
}
public static void question3() {
double[] d = new double[] { 1, 2, 3, 4, 5 };
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (int i = 0; i d.length; i++) {
if (min d[i]) {
min = d[i];
}
if (max d[i]) {
max = d[i];
}
}
System.out.println("最大值" + max);
System.out.println("最小值" + min);
}
public static int average(int[] array) {
int total = 0;
for (int i = 0; i array.length; i++) {
total += array[i];
}
return total / array.length;
}
public static double average(double[] array) {
double total = 0;
for (int i = 0; i array.length; i++) {
total += array[i];
}
return total / array.length;
}
public boolean equals(int[] list1, int[] list2) {
if (list1 == null) {
return false;
}
if (list2 == null) {
return false;
}
if (list1.length != list2.length) {
return false;
}
for (int i = 0; i list1.length; i++) {
if (list1[i] != list2[i]) {
return false;
}
}
return true;
}
Q4: JAVA程序设计题(很简单的)
你的题有很多错误,我给你改了一下。
1.设变量i和j的定义如下,试分别计算下列表达式的值:
int i=1; double d=1.0;
1题 35/4 [8]
2题 46%9+4*4-2 [15]
3题 45+43%5*(23*3%2)[48]
4题 45+45*50%i-- [45]
5题 45+45*50%(i--) [45]
6题 1.5*3+(++d) [6.5]
7题 1.5*3+d++ [5.5]
8题 i+=3/i+3 [7]
程序阅读题
1给定如下代码,写出程序运行结果
class Example{
public static void main(String arges[]){
int i=0;
do{
System.out.println("doing it for i is:"+i);
}while(--i0);
System.out.println("finish");
}
}
结果如下:
doing it for i is:0
finish
2 阅读程序段写出执行结果
for(int k=1;k=5;k++){
if(k4)break;
System.out.println("k="+k);
}
结果:
k=1
k=2
k=3
k=4
3试写出下列程序段循环的运行结果
int i=1;
while(i10)
if(i++%2==0)
System.out.println(i);
结果:
3
5
7
9
操作题
求1!+2!+...+10!
public static void main(String arges[]){
long sum = 0;
for(int i = 1; i = 10; i++) {
long s = 1;
for(int j = 1; j = i; j++) {
s *= j;
}
sum += s;
}
System.out.println("sum = " + sum);
}
求100之内的所有“完数”。完数是指等于它的因子和的数。例如:6=1+2+3,6=1*2*3,则6是一个完数
public class wanshu{
public static void main(String[] args) {
for(int i = 1; i = 100; i++) {
if(fun(i)) {
System.out.println(i);
}
}
}
public static boolean fun(int num) {
int sum = 0;
for(int i = 1; i num; i++) {
if(num % i == 0) {
sum += i;
}
}
return num == sum;
}
}
Q5: 下列java代码中变量a,b,c分别在内存的什么区域?
选2。
a是A的对象的成员。一个对象是在堆中创建的,对象的成员就保存在对象中。
b是运行时创建的局部变量,在methodB方法被调用时在栈中创建。
c是常量,JVM会把常量放在一块独立的堆空间中。
java写代码变量例题的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java变量名的命名规则、java写代码变量例题的信息别忘了在本站进行查找喔。






