问题 问答题

下面是一个Applet程序,其功能是分析任意输入的字符串,将字符串中由空格隔开的字串提取并显示出来。要求,窗口中有一个输入文本框,用于接受输入的字符串,一个按钮,点击则开始分析,分析结果用图形方式打印在窗口下面的区域中。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 程序运行结果如下:

import java.awt.*; import java.applet.*; import java.util.StringTokenizer; public class ex27_3 extends Applet{ private TextField tf; private Button btn; public void init(){tf = new TextField(25);add(tf);btn= new Button("分析");add(btn);resize(250, 200); } public void paint(Graphics g){String str = tf.getText();StringTokenizer st = new StringTokenizer(tf);int n = 80;while (!st.hasMoreTokens()){ String tem = st.nextToken(); g.drawString(tem, n, 80); n += 20;} } public boolean action(Event e, Object o){if (o == "分析") { repaint(); return true;}else return false; } } ex27_3.html <HTML> <HEAD> <TITLE>ex27_3</TITLE> </HEAD> <BODY> <applet code="ex27_3.class" width=800 height=400 </applet> </BODY> </HTML>

答案

参考答案:

解析:new StringTokenizer(str) St.hasMoreTokens() g.drawString(tem, 80,n) 本题主要考查字符串的分割和Applet图形界面相结合的综合应用。解题关键是熟悉字符串的分割方法,使用 StringTokenizer的对象来对String类的对象进行分割。本题中,第1处,StringTokenizer类的构造方法应该以String类的对象作为入口参数;第2处,判断语句应该是当对象st中还有分割串的时候则继续进行;第3处,drawString()方法的参数第2个表示输出字符串起始位置的横坐标,第3个是纵坐标,如果n作为横坐标,则输出的字符串会发生重叠。

阅读理解

On a hot summer day, a soft drink can really take away your thirst. At home, at school, in the park or at the movies---you can find one almost anywhere.

Carbonated

You can see bubbles(泡沫) in these! Of all soft drinks sold, about 75 percent are carbonated(碳酸的). But be careful, carbonated soft drinks have lots of sugar and caffeine(咖啡因). Caffeine is not very good for you in the summer because it takes water out of your body. And the sugar can make you overweight if you drink too much.

Fruit juice

There are different kinds of juice. One kind is made only from fresh fruit. This kind of fruit juice may not taste sweet enough for some people, but it is nutritious(有营养的). Another kind of fruit juice tastes good but has lots of sugar in it. Many young people buy this kind because the advertisements are good. It is bad for your teeth and bones to drink lots of this.

Energy drinks

These kinds of drinks have caffeine and things that make people get excited in them. They are sometimes called “party drinks”. Doctors say that if you drink too much of this kind, you may have a heart attack.

Bottled water

We have lots of different kinds of bottled water, such as mineral water, purified water and distilled water(蒸馏水). They are clean and easy to drink. But it is best not to drink very cold water. It may make your stomach feel bad.

Sports drinks

Most of these have funny names like “Scream(尖叫)” and “G-Vital(激活)”. They have lots of vitamins and minerals in them. After you play sports, you may want to drink one of these.

But if you just spend your summer holiday sitting around watching TV, you won’t need them at all!

小题1:Which two types of drinks may make you overweight?

A.Carbonated and fruit juice.

B. Energy drinks and sports drinks.

C.Carbonated and energy drinks.

D.Fruit juice and bottled water.小题2: Which one may lead to your heart disease?

A.Carbonated.

B.Fruit juice.

C.Energy drinks.

D.Sports drinks小题3: After sports, you have sports drinks because______.

A.they can take away your thirst

B.they can put back minerals you’ve lost in sweat

C.they have funny names that sound very exciting

D.they have no sugar小题4:The writer wrote this passage to______.

A.warn us to keep away from soft drinks

B.teach us some useful ways of keeping healthy

C.tell us how to choose a good one from all kinds of drinks

D.make the advertisements for soft drinks

问答题 简答题