Topic: 请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题

  Print this page

1.请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题 Copy to clipboard
Posted by: 茂珊
Posted on: 2006-04-05 22:15

这是一个要求输入 A 对应 输出 Z 输入B 对应输出 Y
以此类推 ,包括大小写。。。
谁能帮我看一下 关于算法的部分,这个程序可以编译成功,只是算法好像有问题,输入 a 后 点 确定 米有反应阿 !!!
请指教阿

import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;
public class Relative extends Applet implements ActionListener
{
Label s ;
TextField input;
Button btn;
public void init()
{
s = new Label("请输入一个字母!");
input = new TextField(4);
btn = new Button("确定");
add(s);
add(input);
add(btn);
btn.addActionListener(this);

}
public void paint (Graphics g)
{
int n=0;
char c = ' ';
n=Integer.parseInt(input.getText());

c = input.getText();
if(c>97)
{
n=97+(122-n);
c=(char)n;
g.drawString("对应的字母为:"+c,200,200);
}
else if(n>65&&n<90)
{
n = 65+(90-n);
c = (char)n;
g.drawString("对应的字母为:"+c,200,200);
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btn)
repaint();
}
}

2.Re:请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题 [Re: 茂珊] Copy to clipboard
Posted by: 茂珊
Posted on: 2006-04-07 22:09

有没有搞错啊
斑竹都不出来帮忙啊!!
高手门帮帮忙啊

3.Re:请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题 [Re: 茂珊] Copy to clipboard
Posted by: why
Posted on: 2006-04-07 23:04

Why should we help if you provide false information?
这个程序可以编译成功 -- I don't believe this, show me if otherwise.

Moreover, I don't want to spend my time in guessing what you're trying to do?
Explain to us your so called "算法" -- what are those magic numbers 65, 90, 97, 122 -- of course we could check, but why should we waste our time to do so?


input.getText() returns a String, so this is an error:
c = input.getText();

input.getText() returns empty in the initial pass, so this would throw an exception (anyway, your code won't compile):
n=Integer.parseInt(input.getText());

I suggest you to write a command line version before you use an Applet.


//n=Integer.parseInt(input.getText());
//c = input.getText();
String s = input.getText(); // read a String, not character
if (! "".equals(s)) {
c = s.charAt(0); // take the first character
}

if (c >= 97) { //>= 97
c = (char) (97 + (122 - c)); // character would be casted to int for integer arithmetic
g.drawString("对应的字母为:" + c, 200, 200); // resize the applet window or try 50,50 if you can't see the text
} else if (c >= 65 && c <= 90) { //>= 65 && <=90
c = (char) (65 + (90 - c));
g.drawString("对应的字母为:" + c, 200, 200);
}

4.Re:请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题 [Re: why] Copy to clipboard
Posted by: 茂珊
Posted on: 2006-04-08 11:56

谢谢WHY 啦 !!!
下次写上注释就是了。。。BlushBlush
不过 c = input.getText(); 不能默认将读入的第一个字符 赋给 c 吗?
还是Java 中就不能读入 字符阿?

5.Re:请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题 [Re: 茂珊] Copy to clipboard
Posted by: why
Posted on: 2006-04-08 12:11

茂珊 wrote:
下次写上注释就是了。。。BlushBlush
不过 c = input.getText(); 不能默认将读入的第一个字符 赋给 c 吗?
还是Java 中就不能读入 字符阿?

I meant these:
if(c>97)
{ ... }
else if(n>65&&n<90)
what would you use c in the "if" and then "n" in the "else if"?

Why should it 能默认将读入的第一个字符? Why?
Why don't you read the API doc?

And it doesn't mean that "Java 中就不能读入字符".Dead

6.Re:请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题 [Re: 茂珊] Copy to clipboard
Posted by: jeason1914
Posted on: 2006-04-08 12:36

SmileSmile

package org.jeason.test1;
import java.io.*;

public class testfunction {
  
  public static void main(String[] args){
      
        System.out.println("请输入一个字母...");
        try{
          System.out.println(new testfunction().charChangeFunction(System.in.read()));
        } catch (Exception ex){
          ex.printStackTrace();
        }
  }
  
  String charChangeFunction(int charInt) throws Exception{
    String result = "输入了非字母";
    char[] letterArr = new char[26];
    for (int i = 0; i < letterArr.length; i++)
      letterArr[i] =(char)(65 + i);//大写字符
      
    int postion = this.charIndexInArray(charInt,letterArr);
    
    if (postion != -1){
      try {
        result = this.changeCharResult(charInt,postion,letterArr);
      } catch (Exception ex){
        throw new Exception(ex.getMessage());
      }
    }
    return result;
  }
  
  int charIndexInArray(int inputInt,char[] arrChar){
    int result = -1;
    if(arrChar != null){
      
      for(int i = 0;i < arrChar.length; i++){
        if (inputInt <= (int)('Z')){
          if (inputInt == (int)arrChar[i]){
            result = i;
            break;
          }
        } else {
          if (inputInt - 32 == (int)arrChar[i]){
            result = i;
            break;
          }
        }
      }
    }
    
    return result;
  }
  
  String changeCharResult(int inputInt,int postionInt,char[]arrChar)throws Exception{
    if(arrChar != null && inputInt > -1 && postionInt >-1){
      int count = arrChar.length;
      int curIndex = count - postionInt -1;
      char reChar = arrChar[curIndex];
      if (inputInt >= (int)('a'))
        reChar = (char)(((int)reChar) + 32);
      return String.valueOf(reChar);
    } else
      throw new Exception("输入了非字母");
  }
}



7.Re:请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题 [Re: 茂珊] Copy to clipboard
Posted by: mliwng
Posted on: 2006-04-09 18:33

a的asc码+z的asc码=b的asc码+y的asc码=c的asc码+x的asc码
依次类推,可以很简单的写出的

8.Re:请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题 [Re: 茂珊] Copy to clipboard
Posted by: jeason1914
Posted on: 2006-04-11 12:46

高斯的搞法
有新意

9.Re:请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题 [Re: 茂珊] Copy to clipboard
Posted by: jokeyoung
Posted on: 2006-06-02 13:28

import java.text.DecimalFormat;
import java.io.*;
class formartnum
{
public String formart(String str){
double insert=Double.parseDouble(str);
DecimalFormat df=new DecimalFormat("$0,000,000");
return df.format(insert);
}
}
public class formart
{
public static void main(String[] args)
{
formartnum f= new formartnum();

BufferedReader bt=new BufferedReader(new InputStreamReader(System.in));
String str=null;
try{
System.out.print("请输入数据:");
str=bt.readLine();
}catch(IOException e){
e.printStackTrace();
}
//String str="12345678987654";
System.out.println("Message:"+f.formart(str));
}
}

10.Re:请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题 [Re: 茂珊] Copy to clipboard
Posted by: dracularking
Posted on: 2006-06-12 17:21

import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;

public class Applet1 extends Applet implements ActionListener
{
Label s;
TextField input;
Button btn;

public void init()
{
s = new Label("pls input an alphabet:");
input = new TextField(4);
btn = new Button("confirm");
add(s);
add(input);
add(btn);
btn.addActionListener(this);
}

public void paint(Graphics g)
{
int n = 0;
char c = ' ';

String s = input.getText(); // read a String, not character

if (!"".equals(s))
{
c = s.charAt(0); // take the first character
n = (int) c;
}

if (n >= 97 && n <= 122)
{
n = 122 - (n - 97);
c = (char) n;
g.drawString("response alphabet:" + c, 200, 200);
}
else if (c >= 65 && c <= 90)
{
n = 90 - (n - 65);
c = (char) n;
g.drawString("response alphabet:" + c, 200, 200);
}
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btn)
{
repaint();
}
}
}

11.Re:请教,关于输入A 或 a 输出 Z或z ,B---Y;C--X 之类的问题 [Re: 茂珊] Copy to clipboard
Posted by: zz_china
Posted on: 2006-06-13 12:44

用if
if ('a') output 'z'
if ('b') output 'y'
if ('c') output 'x'
...


   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