문제

I am having some trouble with some extra credit stuff my AP Computer Science teacher assigned. Here are the instructions: Write a WordScrambler method recombine. This method returns a String created from its two String parameters as follows:

  • take the first half of word1
  • take the second half of word2
  • concatenate the two halves and return the new string

For example, the following lines show some results of calling recombine. Note that if a word has an odd number of letters, the second half of the word contains the extra letter.

here are the examples it gives:

"apple" + "pear" = "apar" "pear" + "apple" = "peple"

it then says to create method recombine below, only giving one line of code: private String recombine(String word1, String word2) then I have to create the rest. I made a test program just to see if my ideas were correct, that I could use length() and substring() to do it (I know I will have to use them some time in it), then I would change the code to work with the private String recombine(word1,word2) bit. So here is my test code:

public class test
{
    private static String word1,word2;
    public static void main(String[]args){
       word1 = "apple";
       word2 = "pear";
       int length1 = word1.length();
       int length2 = word2.length();
       System.out.println(length1);
       System.out.println(length2);
       int half1 = (int)Math.ceil(length1 / 2);
       int half2 = length2 / 2;
       System.out.println(half1);
       System.out.println(half2);

    }
}

the output is: 5 4 2 2 As you can see, the variable half1 should be 2.5, but since you cant use half of a letter, it should round up, but even with (int)Math.ceil it still rounds down. When I take out the int, just to see if it works as a double, it gives me an error, 'Possible loss of precision, required:int; found:double;`

도움이 되었습니까?

해결책 2

The reason your code does not round down is that the division length1 / 2 happens before Math.ceil call, and it happens in integers.

You can round up division without using Math.ceil. For that you add a number that is less than the divisor by one to the number being divided. For example, if you want to round up the result of division by ten, add nine to the dividend:

int res = (d + 9) / 10; // Rounds up

In your case, all you need is to add one to length1:

int half1 = (length1+1) / 2;

다른 팁

The general answer to your question, for the quotient of two positive numbers a and b, is to compute(*) (a + b - 1) / b with the truncating division that most programming languages have. This produces the integer immediately above the real a / b.

In your example, b is 2. The value of length1 / 2 rounded up can be computed as (length1 + 1) / 2.

(*) This is assuming that a + b - 1 can be computed without overflow, which should not be a problem in your example.

The immediate solution is just to do the division in floating point:

int half1 = (int)Math.ceil(length1 / 2.0);

2 is an int literal and 2.0 is a double literal. Java will automatically promote length1 to double in the presence of 2.0 so this is effectively the following:

int half1 = (int)Math.ceil((double)length1 / 2.0);

But as noted by the other two answers, you don't need to use floating point. Furthermore, double only has 53 bits of integral precision so while doing the division in floating point works for an int it may result in error for a long.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top