Fifth Class blog post and hands-on brain

Source: Internet
Author: User

Java Fifth Class test report
    • 1. randomly generate A number of numbers, populate an array, and then display the array contents with a message box, then calculate the array elements and display the results in a message box.

Program Source code:

Randomly generates 10 numbers, fills an array, then displays the array contents with a message box, computes the array element's and displays the result in a message box.

Wang Hongwei, 2015,10,28

Package test;

import javax.swing.*;

Public class Showarrays

{

Public Static void Main (string[] args)

{

int testarray[] = new int[10];

for (int i = 0;i < 10;i++)

{

int random = (int) (Math. random () * 100);//Generate a 1 to 100 stochastic integer

Math.random generates a double type of data within 1

Testarray[i] = random;

}

int sum = 0;

for (int i = 0;i < 10;i++)

sum = sum + testarray[i];//array sum

Joptionpane. Showmessagedialog (null, "array element is" +testarray[0]+ "" +testarray[1]+ "" +testarray[2]+ "" +testarray[3]

+ "+testarray[4]+" \ n "+testarray[5]+" "+testarray[6]+

"" "+testarray[7]+" "+testarray[8]+" "+testarray[9]+" \ n "+" The sum of the elements of the array is \ t "+sum,

"Randomly generated array", Joptionpane. information_message);

}

}

Program Run Result:

Programming Ideas:

Call the Math.random () function to generate a 0-1 float of type double, multiply the generated random number by 100 to get an integer within 100, and then assign the resulting integer to the element in the array and output it as a dialog box.

Program Flowchart:

2. Turn the numbers into Chinese characters by hands-on brain:

Program Source code:

Write a program to convert an integer to a kanji read string. For example, "1123" is converted to "1123

Wang Hongwei, 2015,10,28

Package test;

Import javax.swing.*;

public class Inttochinesecharacter

{

publicstatic void Main (string[] args)

{

TODO auto-generated method stubs

Stringinput,output = "";//The number entered by the user and the last converted Chinese

Stringchinesecharacter[] = {"Tens of billions", "1 billion", "million", "tens", "million", "100,000", "Million", "thousand", "Hundred", "ten", ""};

Stringchinesenum[] = {"One", "two", "three", "four", "five", "six", "seven", "eight", "Nine"};

input = Joptionpane.showinputdialog (null, "Please enter an integer", "Welcome to this program", 3);

The Joptionpane.showinputdialog method returns the string entered by the user.

The title, message, and icon displayed in the Input dialog box are determined by the parameters passed to the method.

The second parameter is the string to display in the Input dialog box, and the third parameter is the string to display in the title bar of the input dialog box.

The fourth parameter is the icon to be displayed, and the value is the Joptionpane class constant. 0 is Error 1 is normal information 2 is warning 3 is a question

The value of the 1th parameter is null to indicate that the dialog box is displayed in the center of the screen.

int inputnum = integer.valueof (input);//Convert the user input string to an integer

int getnum[] = new Int[input.length ()],i =0;//The number of users input, local variable initialization

while (true)

{

getnum[i]= (inputnum% 10);

i++;

Inputnum= INPUTNUM/10;

if (inputnum== 0)

Break

}//from high to low take each number and store it in getnum[] The array now holds the inverse of the current number.

int length = getnum.length;//to find out the length of the Inputnum, which determines which one to start reading from

for (i = 0;i < length;i++)

{

Intindex;

index= Getnum[i];

getnum[i]= getnum[length-1-i];

Getnum[length-1-I] = index;

}//to convert the array in reverse order.

for (i = 0;i < length;i++)

{

output+= Chinesenum[getnum[i]-1];

output+= chinesecharacter[chinesecharacter.length-length + i];

}

Joptionpane.showmessagedialog (null, Output, "translated into Chinese result is", 1);

}

}

Program Run Result:

Fifth Class blog post and hands-on brain

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.