Java编程begginers(数组) [英] Java programming begginers (Arrays)

查看:68
本文介绍了Java编程begginers(数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java的新手,我需要帮助来完成下面的问题。如果有人能帮助我解决这个问题的简单方法。我试图提供一个有效的解决方案,但我遇到了数字#10。



1.创建一个名为Rebel.java的新文件;



2.创建两个数组。其中一个必须存储整数,另一个必须存储单词。他们每个人必须能够存储5个值;



3.填充第一个数字,数字从1到5,第二个数字从6到10;注意:单词数组应包含字符串形式的值,而不是实际的单词,即 - >单词[1] =1;



4.将第一个数组中的数字与第二个数字中的数字交换(交换数字具有相同的索引);



5.将第一个数组中的所有数字乘以4;



6.Multiply all第二个数组中的数字加3;



7.打印出两个数组的内容。



8.将第一个数组中的所有数字与第二个数字中的数字相交,但这次只交换偶数位置的数字。



9.对于每个数字来自如果它存储在存储数字的数组中,则打印出1到40,存储单词或不存储单词的数组。



10.对于数字数组中的每个数字:

•打印出来;

•打印出数字数组中除数的位置。

•打印出单词数组中除数的位置。



11.重复#10的单词数组。



这是我的代码:



  public   class  Rebel 
{
public static void main( String [] args)
{

int [] numbers = { 1 2 3 4 5 };
字符串 [] words = { 6 7 8 9 10};



个数字[ 0 ] = 6 ;

System.out.println( \ n数字中的值为:< /跨度>);
for int x = 0 ; x< numbers.length;> {
numbers [x] = numbers [x] * 4 ;
System.out。 println( + numbers [x]);
}

words [ 0 ] = 1;

System.out.println( \ n单词中的值为:);
for int x = 0 ; x< words.length;> {
int wordsAsAnInt = Integer.parseInt(words [x]) ;
wordsAsAnInt = wordsAsAnInt * 3 ;
System.out.println ( + wordsAsAnInt);
}

数字[ 0 ] = 6 ;
字[ 0 ] = 1 ;
数字[ 2 ] = 8 ;
字[ 2 ] = 3 ;
数字[ 4 ] = 10 ;
字[ 4 ] = 5 ;

System.out.println( \ n数字从1到40的值是:);
for int x = 0 ; x< numbers.length;> {
if (numbers [x] == 1 || numbers [x]< 40
{
System.out.println( + numbers [x]);
}
}

System.out .println( \ n数字中的值为:);
for int x = 0 ; x< numbers .length;> {
System.out.println( + numbers [x ]);
}
}

}

解决方案

其他e,

据我所知,我会说你在#8上失去了自己的代码,你应该在数组之间交换数字吗?那么为什么要插入值而不是从数组交换到数组呢?



数字9是错误的,因为如果数字在数字数组,文本数组或中,则应打印没有一个。你的想法是正确的,但我建议改变代码



  for  int  i =  0 ; i <   40 ; i ++)
{
bool found = ;
for int x = 0 ; x < numbers.length; x ++)
{
if (i == numbers [x]){System。 out .println(i + 是数字); found = true ;}
}
for int x = 0 ; x < words.length; x ++)
{
// 从字符串转换为int!
if (i ==( int )words [x]){System。 out .println(i + 以单词); found = true ;}
}
if (!found){System。 out .println(i + 不在数组);}
}





#10是我的东西到目前为止还不明白,你的作业是显示数组中每个数字的除数还是你必须打印出数组中最后一个数组的除数?


I'm new to java and I need help completing the question below. If anyone could help me with a simpler solution of this question. I tried to provide a working solution but I got stuck at number #10.

1.Create a new file called Rebel.java;

2.Create two arrays. One of them must store integer numbers and the other one must store words. Each of them must be able to store 5 values;

3.Fill the first array with numbers from 1 to 5 and the second one with numbers from 6 to 10; Note: The words array should contain the value in the form of a string and not the actual word i.e -> words[1] = "1";

4.Swap the numbers in the first array with the ones in the second one (swap numbers with the same index);

5.Multiply all the numbers in the first array by 4;

6.Multiply all the numbers in the second array by 3;

7.Print out the contents of both arrays.

8.Swap all the numbers in the first array with the ones in the second one but this time only swap the numbers at even positions.

9.For every number from 1 to 40 print out if it is in the array that stores numbers, the one that stores words or none of them.

10.For every number in the numbers array:
•Print it out;
•Print out the positions of the divisors it has in numbers array.
•Print out the positions of the divisors it has in words array.

11.Repeat #10 for the words array.

Here's my code:

public class Rebel
{  
     public static void main(String [] args)  
     {

         int[] numbers = {1,2,3,4,5};
         String[] words = {"6","7","8","9","10"};



      numbers[0] = 6;  

      System.out.println("\nThe values in the numbers are:");
      for(int x = 0; x<numbers.length;>      {
         numbers[x] = numbers[x] * 4;
         System.out.println(" " + numbers[x]);
      } 

      words[0] = "1";

      System.out.println("\nThe values in the words are:");
      for(int x = 0; x<words.length;>      {
         int wordsAsAnInt = Integer.parseInt(words[x]);
         wordsAsAnInt = wordsAsAnInt * 3;
         System.out.println(" " + wordsAsAnInt);
      }

      numbers[0] = 6;
      words[0] = "1";
      numbers[2] = 8;
      words[2] = "3";
      numbers[4] = 10;
      words[4] = "5";

      System.out.println("\nThe values in numbers from 1 to 40 are:");
      for(int x = 0; x<numbers.length;>      {
         if(numbers[x] == 1 || numbers[x] < 40)
         {
            System.out.println(" " + numbers[x]);
         }
      }

      System.out.println("\nThe values in the numbers are:");
      for(int x = 0; x<numbers.length;>      {
         System.out.println(" " + numbers[x]);
      } 
   }

}

解决方案

Hi there,
as far as i could read that code i'd say you lost your self on #8, you should swap the numbers between arrays ? So why do you insert values instead of a swap from array to array?

Number 9 is wrong because you should print if number is in numbers array, text array or in none of them. Your idea is correct but i'd suggest this change in code

for(int i = 0; i < 40; i++)
{
    bool found = false;
    for(int x = 0; x < numbers.length; x++)
    {
        if(i == numbers[x]) {System.out.println(i + " is in numbers"); found = true;}
    }
    for(int x = 0; x < words.length; x++)
    {
        //Do a conversion from string to int!
        if(i == (int)words[x]) {System.out.println(i + " is in words"); found = true;}
    }
    if(!found) {System.out.println(i + " is in none of the arrays");}
}



#10 is something i don't understand so far, is your homework to show the divisors of every number in the array or do you have to print out the divisor of the last array number which is in the array?


这篇关于Java编程begginers(数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆