Topic: 一个初学的练习题,帮忙给点思路,现在只学到基本数据类型

  Print this page

1.一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 Copy to clipboard
Posted by: binge
Posted on: 2006-01-22 01:15

写一程序,用以判断一个五位整数N的各位数字的奇偶性,并求出它们的和,运行结果提示:
偶数个数:2
奇数个数:3

2.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: binge] Copy to clipboard
Posted by: binge
Posted on: 2006-01-22 01:29

还有一个

写一程序,用于判断一个整数n(100<n<1000)的奇偶性,如果为偶,输出大写A,ELSE输出大写B。

3.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: binge] Copy to clipboard
Posted by: binge
Posted on: 2006-01-22 01:34

自已想了一个
第二题的

    int a=120;
    int b=a%2;
    System.out.println((100<a)&&(a<1000)&&(b==0)?"A":"B");

大家看下对不对

还有高见就写出来哈

4.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: binge] Copy to clipboard
Posted by: why
Posted on: 2006-01-22 04:20

> 写一程序,用以判断一个五位整数N的各位数字的奇偶性,并求出它们的和,运行结果提示:
> 偶数个数:2
> 奇数个数:3
Where is 它们的和?

> (100<a)&&(a<1000)&&(b==0)?"A":"B"
Now, not matter n is odd or even, it will result in "B" when n is out of the range.

5.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: binge] Copy to clipboard
Posted by: kof821117
Posted on: 2006-01-23 01:29


import java.io.*;
class count{

int num;
String on;
String en;
//String num1;
// void result(){
/// System.out.println("The num is "+on);
// System.out.println("The num is "+en);
// }
// public void setnumber(){
// if (num%2>0){
// //


// }
// }
}

public class accout{

public static void main() throws IOException
{
count rect1=new count();
// rect1.num;
rect1.on="odd number";
rect1.en="even number";
String str;
BufferedReader buf;

buf = new BufferedReader(new InputStreamReader (System.in));

System.out.println("Input a number: ");
str = buf.readLine();
rect1.num=Integer.parseInt(str);


if ((rect1.num>0 )&& (rect1.num % 2==0) ){



System.out.println("The num is "+rect1.en);

}
else
System.out.println("The num is "+rect1.on);


}

}

我自己写的,看看,或许是这个思路~~如果有缺点请提出来

6.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: kof821117] Copy to clipboard
Posted by: why
Posted on: 2006-01-23 02:10

kof821117 wrote:
我自己写的,看看,或许是这个思路~~如果有缺点请提出来

1. bad class names, especially accout
The first character of the class name should better be uppercase

2. poor formatting (after I added the [ code ] tags for you)

3. public static void main()
Shouldn't it be public static void main(String[] args)
Have you tested you code?

4. count rect1=new count();
// rect1.num;
rect1.on="odd number";
rect1.en="even number";
bad member names, bad style for initialization,
in fact, class "Count" is unnecessary.

5. ......

7.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: why] Copy to clipboard
Posted by: binge
Posted on: 2006-01-23 13:35

why wrote:
> 写一程序,用以判断一个五位整数N的各位数字的奇偶性,并求出它们的和,运行结果提示:
> 偶数个数:2
> 奇数个数:3
Where is 它们的和?

> (100<a)&&(a<1000)&&(b==0)?"A":"B"
Now, not matter n is odd or even, it will result in "B" when n is out of the range.


frist my answer is for the second one
only beginner,there is not decision the n's range

8.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: kof821117] Copy to clipboard
Posted by: binge
Posted on: 2006-01-23 13:45

kof821117 wrote:
我自己写的,看看,或许是这个思路~~如果有缺点请提出来


功能达到了,不符合题目要求。

据why 兄看来,这个代码还有N多错误啊。

额外的一个问题,本论坛里的代码不能COPY啊!
没有自动换。

9.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: binge] Copy to clipboard
Posted by: binge
Posted on: 2006-01-23 14:13

why 兄:
第一个题该怎么作呢。我还在还没有一点思绪

10.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: binge] Copy to clipboard
Posted by: joe031102
Posted on: 2006-01-23 17:23

这是第一个的1解,欢迎批评斧正。小弟初学java。gg们多多指点。谢谢大家
五位数字是可配置的,不是读取的。


public class JudgeOddEven {

  private static int WUWEISHUZI = 54321;
  private static int i;
  private static int g;  
  private static int gs;
  private static int s;
  private static int gsb;
  private static int b;
  private static int gsbq;
  private static int q;
  private static int w;
  private static int countOdd = 0;
  private static int countEven = 0;
  private JudgeOddEven(){
    
  }
  
  private void business(){
    i = WUWEISHUZI;
    g = i%10;
    System.out.println("***g = " + g);
    gs = i%100;
    s = (gs - g)/10;
    System.out.println("***s = " + s);
    gsb = i%1000;
    b = (gsb - gs)/100;
    System.out.println("***b = " + b);
    gsbq = i%10000;
    q = (gsbq - gsb)/1000;
    System.out.println("***q = " + q);
    w = (i - gsbq)/10000;
    System.out.println("***w = " + w);  
  }
  
  private void process(int j){
    if ( j == 2){
      System.out.println("******" + j + "is an Even.");
      countEven ++;
    }else if ( j > 2 && j % 2 == 0){
      System.out.println("******" + j + "is an Even.");
      countEven ++;
    }else{
      System.out.println("******" + j + "is an Odd.");
      countOdd ++;
    }
  }
  private int result(){
    int p = g + s + b + q + w;
    return p;
  }
  private void display(){
    business();
    process(g);
    process(s);
    process(b);
    process(q);
    process(w);
    System.out.println("The sum of the five figure is: " + result());
    System.out.println("The Number of Odd is: " + countOdd);
    System.out.println("The Number of Even is: " + countEven);
  }
  /**
   * @param args
   */
  public static void main(String[] args) {
  
    JudgeOddEven j = new JudgeOddEven();
    j.display();
  }

}

11.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: binge] Copy to clipboard
Posted by: why
Posted on: 2006-01-24 02:33

binge wrote:
额外的一个问题,本论坛里的代码不能COPY啊!
没有自动换。

敝人用Firefox沒問題,剛才試試IE,的確"没有自动换"
閣下可以在"站务管理"提出

 
謝謝joe031102熱心回應,不過...有待改進

package net.cjsdn.why;

public class JudgeOddEven {
private int inputNumber;
private int[] digits;
private int numDigits = 5;
private int countOdd = 0;
private int countEven = 0;
private int sumOfDigits = 0;

public JudgeOddEven() {
digits = new int[numDigits];
}

public JudgeOddEven(int numDigits) {
if (numDigits >= 1) {
this.numDigits = numDigits;
}
this.digits = new int[numDigits];
}

public int getCountEven() {
return countEven;
}

public int getCountOdd() {
return countOdd;
}

public int getSumOfDigits() {
return sumOfDigits;
}

  public int getInputNumber() {
    return inputNumber;
  }

public void setInputNumber(int number) {
//TODO: check whether number has the correct number of digits
inputNumber = number;
sumOfDigits = 0;
countEven = 0;
countOdd = 0;

//TODO: comment on div1 & div2
for (int i = 0, div1 = 1, div2 = 10; i < numDigits; i++) {
digits[i] = (inputNumber % div2) / div1;
div1 = div2;
div2 *= 10;

sumOfDigits += digits[i];

if ((digits[i] % 2) == 0) {
countEven++;
} else {
countOdd++;
}
}
}

public void printResult() {
System.out.println("数字:" + inputNumber);
System.out.println("偶数个数:" + countEven);
System.out.println("奇数个数:" + countOdd);
System.out.println("各位数字的和" + sumOfDigits);
}

public static void main(String[] args) {
JudgeOddEven joe = new JudgeOddEven(5);
joe.setInputNumber(12345);
joe.printResult();
}
}

12.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: binge] Copy to clipboard
Posted by: joe031102
Posted on: 2006-01-24 08:29

元老果然就是元老,Smile。谢谢你的指点。我继续努力,嘿嘿。

13.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: joe031102] Copy to clipboard
Posted by: why
Posted on: 2006-01-24 08:40

joe031102 wrote:
元老果然就是元老,Smile。谢谢你的指点。我继续努力,嘿嘿。

批評他人是很容易的,事實上,敝人上面的急就章也頗有可議之處.
不過,還是希望閣下注意以下幾點:

1.為何要用static?
private static int i;
...
private static int w;

private int result()

1.1.member variable的名字也不好
 
1.2.奇怪的method名字
private void business()

2.為啥要特別處理j==2?
if ( j == 2){
System.out.println("******" + j + "is an Even.");
countEven ++;
}else if ( j > 2 && j % 2 == 0){
System.out.println("******" + j + "is an Even.");
countEven ++;

之前少貼了這一點:
3.s = (gs - g)/10;等
應該不難看出gs / 10 == (gs - g) / 10

14.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: binge] Copy to clipboard
Posted by: joe031102
Posted on: 2006-01-24 08:58

為啥要特別處理j==2?这个是我多虑了,static的问题是原来需要static的, 后来忘记把它们去掉了。Smile

15.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: binge] Copy to clipboard
Posted by: binge
Posted on: 2006-01-26 09:31

谢谢why
谢谢joe031102

由于刚学java
在有些地方不清楚

以后大家多多提点啊

16.Re:一个初学的练习题,帮忙给点思路,现在只学到基本数据类型 [Re: why] Copy to clipboard
Posted by: konn
Posted on: 2006-03-05 11:02


public class Hello
{ public static void main(String args[])
{ int i,j;
j=0;
i=0;
int countofbit,countofodd,countofeven,sumofodd,sumofeven;
int a=1258063320;
countofodd=0;
countofeven=0;
sumofodd=0;
sumofeven=0;
countofbit=Integer.toString(a).length();
for (j=0;j<countofbit ;j++ )
{i=a%10; //余10,得到一位数
a/=10; //除以10
if (i==0)
{
countofeven--; //偶数应减去0的个数
}
if (i%2==0)
{
countofeven++; //偶数或0,则countofeven加1
sumofeven+=i; //计算偶数和
}
else
{ countofodd++; //奇数,则countofodd加1
sumofodd+=i; //计算奇数和
}
}
System.out.println("总位数:"+countofbit);
System.out.println("偶数个数:"+countofeven);
System.out.println("奇数个数:"+countofodd);
System.out.println("0个数:"+(countofbit-countofeven-countofodd)); //计算0的个数
System.out.println("偶数和:"+sumofeven);
System.out.println("奇数和:"+sumofodd);

}

}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923