Chapter 4 of Java programming ideology exercise 9 & exercise 10

Source: Internet
Author: User

I don't know why. I may not be able to concentrate when I relax. The two simple questions just took an hour and my start point was too low. Although it is necessary to practice programming more, it is also a matter of white. It would be a little more efficient to be fully engaged in your work. Is this the reason?

Exercise 9: output the num item data before the Fibonacci. Of course, int is used here, which will overflow. I marked my mistakes. The details of these things are too difficult to do and I cannot learn them myself.

Public class Fibonacci {public static void output (INT num) {int I = 1, j = 1; if (Num <1) {system. out. println ("Please input number at least 1"); return;} system. out. print (I + ""); While (Num> 1) {system. out. print (J + ""); // an error such as J + ''may occur !!! J = I + J; I = J-I; num --;} return;} public static void main (string [] ARGs) {If (ARGs. length! = 1) {system. Out. println ("Please input a number"); return;} output (New INTEGER (ARGs [0]);}

Exercise 10: Vampire numbers. Output the four-digit vampire number.

public class XiXueGui{public static void main(String[] args){int number=0;Outer:for(int i=1001;i<9999;i++){if(i%100==0){continue;}char[] a;//a=Integer.toString(i).toCharArray();a=(""+i).toCharArray();for(int j=0;j<4;j++)for(int k=0; k<4;k++){if(k==j){continue;}for(int m=0;m<4;m++){if(m==k || m==j){continue;}if(((a[j]-'0')*10+(a[k]-'0'))*((a[m]-'0')*10+(a[6-j-k-m]-'0'))==i){System.out.println(number+" "+i+"="+((a[j]-'0')*10+(a[k]-'0'))+"*"+((a[m]-'0')*10+(a[6-j-k-m]-'0')));number++;continue Outer;}}}}}}

It will be messy, followed by char converted to int, minus '0'. When writing a program, I forget it, so an error occurs.

 

Ah, I have no confidence in myself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.