| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 589 人关注过本帖
标题:使用哨兵控制重复结构问题
只看楼主 加入收藏
shenwc
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2006-2-11
收藏
 问题点数:0 回复次数:7 
使用哨兵控制重复结构问题

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication6
{
class Average2
{
static void Main(string[] args)

{
int total, gradeCounter,gradeValue;

double average;
total = 0;
gradeCounter = 0;
Console.Write("Enter Integer grade,-1 to Quit:");
gradeCounter = Int32.Parse(Console.ReadLine());
if ( gradeCounter == -1 )
{
Console.WriteLine("No grades were entered.");
}
while (gradeValue != -1)
{
total = total + gradeValue;
gradeCounter = gradeCounter + 1;
Console.Write("Enter Integer Grade,-1 to Quit: ");
gradeCounter = Int32.Parse(Console.ReadLine());


average = (double)total / gradeCounter;
Console.WriteLine("\nClass average is {0}", average);
}


}
}
}


请帮我看一下这个问题出错了:提示上说, 使用了未赋值的局部变量“gradeValue”
但是我随便赋了一个值.要不能实现哨兵这个功能.

搜索更多相关主题的帖子: 哨兵 结构 
2006-07-11 22:42
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 

此问题比较有意思
期待答案


此号自封于2006年11月30日
2006-07-12 08:58
ytyt654
Rank: 2
等 级:新手上路
威 望:4
帖 子:195
专家分:0
注 册:2006-2-13
收藏
得分:0 
gradeValue使用之前没赋初值

2006-07-12 09:27
foproxs
Rank: 1
等 级:新手上路
帖 子:132
专家分:0
注 册:2006-4-24
收藏
得分:0 

static void Main(string[] args)

{
int total, gradeCounter,gradeValue;

double average;
total = 0;
gradeCounter = 0;
Console.Write("Enter Integer grade,-1 to Quit:");
gradeCounter = Int32.Parse(Console.ReadLine());
if ( gradeCounter == -1 )
{
Console.WriteLine("No grades were entered.");
}
while (gradeValue != -1)
{
total = total + gradeValue;
gradeCounter = gradeCounter + 1;
Console.Write("Enter Integer Grade,-1 to Quit: ");
gradeCounter = Int32.Parse(Console.ReadLine());


average = (double)total / gradeCounter;
Console.WriteLine("\nClass average is {0}", average);
}


}

给gradeValue一个初值


2006-07-12 10:30
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 

我试过这个程序
定义为成员或者定义为变量都一样
total = total + gradeValue;
这句话执行完之后TOTAL还是一直为0
大家可以试一下
如果提示找不到空间
using System.Collections.Generic;
把这个改为
using System.Collections;
我是这么改的
调试了这个程序之后一直不理解为什么total咋就一直是0呢?


此号自封于2006年11月30日
2006-07-12 12:28
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication6
{
class Average2
{
static void Main(string[] args)

{
int total=0, gradeCounter=0,gradeValue=0;

double average;
Console.Write("Enter Integer grade,-1 to Quit:");
gradeCounter = Int32.Parse(Console.ReadLine());
if ( gradeCounter == -1 )
{
Console.WriteLine("No grades were entered.");
}
while (gradeValue != -1)
{
total = total + gradeValue;
gradeCounter = gradeCounter + 1;
Console.Write("Enter Integer Grade,-1 to Quit: ");
gradeCounter = Int32.Parse(Console.ReadLine());


average = (double)total / gradeCounter;
Console.WriteLine("\nClass average is {0}", average);
}


}
}
}

这样就OK了,既然它告诉你使用了未赋值的变量,你就给它赋值呗


public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-07-12 15:09
shenwc
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2006-2-11
收藏
得分:0 

侠之大者你好.
赋了一个值之后虽然程序没有出什么错.
但是就没有实现哨兵控制重复结构问题.
而且平均值总是0.
这个程序要实现的功能是这样的:
假如你输入一个班的成绩.
你一直输下出.当你输入-1时程序就把平均成绩求出来.


http://bbs./dispbbs.asp?BoardID=117&ID=78965&star=100
2006-07-12 20:53
fengwill99
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-7-13
收藏
得分:0 

晕,这个程序有个性. 两个参数到底在用谁做result?

2006-07-13 19:33
快速回复:使用哨兵控制重复结构问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012153 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved